2
votes

I am using the maven assembly plugin so that I can include specific dependencies(basically I want to include specific transitive dependencies) class in my jar file. Here is my relevant code snippet from the pom.xml -

<build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
                <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>  

      </plugins>
  </build> 

Looks like it is possible from link http://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html But as soon as mention below code snippet to include two types of dependencies com.companyA.* and com.companyB.* ,except that I do not want to exclude all other dependencies

<dependencySets>
    <dependencySet>
      <includes>
        <include>com.companyA.*</include>
        <include>com.companyB.*</include>
      </includes>
    </dependencySet>
  </dependencySets>

But pom.xml says invalid content for dependencySets. I don't know how to achieve objective here?

2

2 Answers

0
votes

I typically use two <dependencySet> entries with mutually exclusive patterns, like so

<dependencySet>
  <outputDirectory>lib</outputDirectory>
  <scope>runtime</scope>
  <excludes>
    <exclude>com.mypkg.*:*</exclude>
  </excludes>
</dependencySet>

<dependencySet>
  <outputDirectory>bin</outputDirectory>
  <scope>runtime</scope>
  <includes>
    <include>com.mypkg.*:*</include>
  </includes>
</dependencySet>

In this case it's copying everything in com.mypkg to the bin folder, and everything not com.mypkg to the lib folder

This kind of approach should suffice for what you're trying to accomplish

1
votes

I think you have the same problem with me. Here you can only do part config in pom.xml like this:

<plugin>    
              <artifactId>maven-assembly-plugin</artifactId>    
              <configuration>    
                    <descriptors>    
                        <descriptor>assembly.xml</descriptor>    
                   </descriptors>    
              </configuration>    
                <executions>  
                    <execution>  
                        <phase>install</phase>  
                        <goals>  
                            <goal>single</goal>  
                        </goals>  
                        <configuration>  
                            <outputDirectory>d:/temp/stock</outputDirectory>  
                        </configuration>  
                    </execution>  
                </executions>  

         </plugin>  

but you can config most of assembly config in assembly.xml, like this:

<assembly  
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">  
    <id>jar-with-dependencies</id>  
    <formats>  
        <format>dir</format>  
    </formats>  
    <includeBaseDirectory>false</includeBaseDirectory>  
    <dependencySets>  
        <dependencySet>  
            <useProjectArtifact>true</useProjectArtifact>  
            <outputDirectory>/</outputDirectory>  
            <includes>  
                <include>org.tkxing.stock:org.tkxing.stock.test</include>  
            </includes>         
        </dependencySet>  
        <dependencySet>  
            <useProjectArtifact>false</useProjectArtifact>  
            <outputDirectory>lib/</outputDirectory>  
            <excludes>  
                <exclude>org.springframework:spring-beans</exclude>  
                <exclude>org.springframework:spring-asm</exclude>  
                <exclude>org.springframework:spring-core</exclude>  
                <exclude>org.springframework:spring-aop</exclude>  
                <exclude>org.springframework:spring-context</exclude>  
                <exclude>org.springframework:spring-expression</exclude>  
                <exclude>org.springframework:spring-jms</exclude>  
                <exclude>org.springframework:spring-tx</exclude>  
            </excludes>  
        </dependencySet>  
    </dependencySets>  

    <fileSets>  
        <fileSet>  
            <directory>conf</directory>  
            <outputDirectory>conf</outputDirectory>  
        </fileSet>  
        <fileSet>  
            <directory>bundles</directory>  
            <outputDirectory>bundles</outputDirectory>  
        </fileSet>  
    </fileSets>  

    <files>  
        <file>  
            <source>log4j.xml</source>  
            <outputDirectory>/</outputDirectory>  
        </file>  
    </files>  

</assembly>  

hope this could help others