I am noticing that maven-assembly-plugin doesnt want to include snapshot dependencies in the assembly jar. Is this by design or am i doing something wrong.
<dependency>
<groupId>com.xxx.zzz</groupId>
<artifactId>projectname</artifactId>
<version>1.7.0-SNAPSHOT</version>
</dependency>
There are multiple snapshot dependencies like this, so I hate to include them explicitly in the assembly descriptor.
Question 1:
Is there a way to include the snapshot dependencies in the bundle too?
Question 2:
Can you help to figure out how to include all dependencies (compile, test, provided and runtime) in the assembled jar . By default, maven considers only the runtime jars
Following is the maven-assembly section in the pom
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${project.basedir}/assembly.xml</descriptor>
</descriptors>
<tarLongFileMode>gnu</tarLongFileMode>
<finalName>${project.artifactId}-${project.version}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Here is assembly descriptor file
<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>dist</id>
<includeBaseDirectory>true</includeBaseDirectory>
<formats>
<format>jar</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<includes></includes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<includes>
<include>database/**</include>
<include>deploy/**</include>
</includes>
<excludes>
<exclude>**/*.tmp</exclude
</excludes>
<directory>${project.basedir}</directory>
</fileSet>
</fileSets>
</assembly>
Could you please shed some light?
as for question 1, to include snapshot dependencies from the current build, use a moduleSet
http://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/module-binary-inclusion-simple.html
that is, if they come from the current reactor. otherwise you will have to build them and depend on a release version.
figured out the answer for 2nd question. Thanks to Bo Ni, who posted this blog: http://bosbluebluesky.blogspot.com/2012/11/maven-package-jar-file-include-all.html
All i had to do was include different scope in multiple dependencyset sections individually
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>runtime</scope>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<includes></includes>
</dependencySet>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>compile</scope>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<includes></includes>
</dependencySet>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>provided</scope>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<includes></includes>
</dependencySet>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>test</scope>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<includes></includes>
</dependencySet>
</dependencySets>
Related
I am trying to include my test classes in a generated JAR in a Maven Project.
I have created the Maven project and created the test classes with JUnit.
When I am trying to build the JAR, these test classes are not included in the generated JAR.
You can produce a jar which will include your test classes and resources.
Please refer maven official site - https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
I have resolved issue be adding things. We will get two jars. One with classes and other with test classes.
assembly.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi chemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd <http://maven.apache.org/xsd/assembly-1.1.3.xsd> ">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
POM.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptor>src/main/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
I want to include scala test case to the jar file. I have maven project.
I have used following :
<assembly>
<id></id>
<formats>
<format></format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>...</include>
</includes>
</dependencySet>
<dependencySet>
<outputDirectory></outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test/scala</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
Can anybody tell me what is the way to add scala testcase into jar file using
mvn clean package
Thanks.
You need to create a fat jar with test-classes and external dependencies. The Maven Assembly Plugin is the obvious choice:
Plugin example:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.example.MainTest</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
You also need to create a descriptor file, in the src\main\assembly folder an assembly.xml file with the following content:
An example:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
Build Jar:
mvn clean compile test-compile assembly:single
Run Jar:
java -jar fat-jar-include-tests.jar
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?
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
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
The following see How can I create an executable jar with dependencies using Maven? shows how to create an executable jar with the Maven plugin.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
However I wish to exclude some of the dependencies which are in my POM. All I want to do is to add
<dependencySets>
<dependencySet>
<useStrictFiltering>true</useStrictFiltering>
<scope>compile</scope>
<excludes>
<exclude>com.excluded:artifact</exclude>
</excludes>
</dependencySet>
</dependencySets>
to the above but I cannot , because I need to add that in a separate assembly descriptor. What I want to do is exactly the same as jar-with-dependencies but with the exclusion. Is there somewhere where an equivalent assembly descriptor file is described so I can edit it? Is there any way to 'inherit' jar-with-dependencies and add my exclusions?
I think that you can use scope "compile" :
<dependency>
<groupId>com.xxxx</groupId>
<artifactId>xxxxxx</artifactId>
<version>3.x</version>
<scope>compile</scope>
</dependency>
See The Maven Documentation on pre defined descriptors for the equivalent of jar-with-depedencies.
So I can change the POM I am using to build 'super jar' as follows;
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>my.Main</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<!-- Uncomment this for automatic creation of the Complete jar
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
-->
</plugin>
And in the directory src/main/assembly/assembly.xml I can put the following which is essentially the same as the jar-with-dependencies descriptor with my exclusions;
<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">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<excludes>
<exclude>com.local.depedency:artifact_id</exclude>
<exclude>transitive.depedency.so.way.down:artifact_id</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
I have had a project set up for the past year or so and have been using the maven assembly plugin to package the jar.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>myProject</finalName>
<descriptors>
<descriptor>src/main/assembly/project.xml</descriptor>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I have been using the project.xml descriptor to specify the exclusions of the jar as follows:
<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>project</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<useTransitiveFiltering>true</useTransitiveFiltering>
<scope>runtime</scope>
<excludes>
<exclude>commons-codec:commons-codec</exclude>
<exclude>commons-io:commons-io</exclude>
<exclude>commons-lang:commons-lang</exclude>
<exclude>org.apache.commons:commons-lang3</exclude>
<exclude>commons-logging:commons-logging</exclude>
...
</excludes>
</dependencySet>
</dependencySets>
This has been working fine. My jar would be packaged perfectly, excluding the dependencies that it was supposed to and would end up only being around 850KB by the end of everything. So today I go to package my jar as normal after doing some updates and none of the exclusions are being applied, and my final Jar is at about 12MB. This was quite alarming and frankly annoying. The thing is I have done absolutely nothing to the pom.xml. I back up my eclipse workspaces every night and I went to back to what I had yesterday (which I know produced a 850KB jar) and its the same result, a 12MB jar. Is there any sort of thing that would change this. An update of some kind? Anything?