Add scala test-case to jar file in maven project - java

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

Related

Create directory with Maven

I am having a Java Project managed by Maven. I want to create .jar file of the project automatically with Maven such that after Maven executes the task, there will be something like this:
directory_i_want_to_create
my_project.jar
config.json
input_folder
As my project needs to read input from input folder and config.json as above (so that I can run the command line to execute the jar), how can I create such directory_i_want_to_create like above with Maven ?
I would recommend an Assembly Plugin. Configuration would looks like:
pom.xml
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/default.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
default.xml
<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>default</id>
<formats>
<format>dir</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>directory_i_want_to_create</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>input_folder</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Small amount less XML if you use Ant
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<target>
<mkdir dir="target/directory_i_want_to_create" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Combining Jar-with-dependencies and a ZIP file using the Maven assembly plugin

I want to combine my source code in a JAR with all dependencies as well as package this up into a zip file along with other files. I'm able to create the one Jar with all dependencies as well as the ZIP file but I can't combine the two.
I ultimately want the following directory structure in the zip file:
loader/bin/shellscript.sh
loader/lib/jar-with-dependencies.jar
loader/appname/config/config.xml
Here's an extract from my pom file:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
Here's an extract from my assembly
<assembly>
<id>bin</id>
<!-- Specifies that our binary distribution is a zip package -->
<formats>
<format>zip</format>
</formats>
<baseDirectory>SpreadsheetLoaderApp</baseDirectory>
<fileSets>
<fileSet>
<directory>corporatebondpurchases</directory>
<outputDirectory>${basedir}/corporatebondpurchases/config</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<fileSet>
<directory>corporatebondpurchases</directory>
<outputDirectory>${basedir}/bin</outputDirectory>
<includes>
<include>*.sh</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Here's the output from part of the build:
[INFO] --- maven-assembly-plugin:2.4:single (default-cli) # SpreadsheetLoader ---
[INFO] Reading assembly descriptor: src/main/assembly/assembly.xml
[INFO] Building zip: C:\Software\SpringSTS\workspace\SpreadsheetLoader\target\SpreadsheetLoader-0.0.1-SNAPSHOT-bin.zip
[INFO] Building jar: C:\Software\SpringSTS\workspace\SpreadsheetLoader\target\SpreadsheetLoader-0.0.1-SNAPSHOT-jar-with-dependencies
This succeeds in creating one Jar under target as well as the ZIP file (albeit with full paths rather than relative paths). What I want is for the Jar file to be included as part of the zip file.
EDIT:
After researching various blog posts I've managed to get this working by using the following POM and assembly files
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And the corresponding assembly file...
<assembly>
<id>dist</id>
<!-- Specifies that our binary distribution is a zip package -->
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<baseDirectory>SpreadsheetLoader</baseDirectory>
<files>
<file>
<source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
<outputDirectory>lib</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>applicationbuild/${buildname}</directory>
<outputDirectory>${buildname}/config</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<fileSet>
<directory>applicationbuild/${buildname}</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*.sh</include>
</includes>
<lineEnding>unix</lineEnding>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>./</directory>
<outputDirectory>logs</outputDirectory>
<excludes>
<exclude>*/**</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>./</directory>
<outputDirectory>${buildname}/sourcedata</outputDirectory>
<excludes>
<exclude>*/**</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
This gives me a nice simple directory structure for deployment purposes. My main source of info came from the following site:
http://www.drawbackz.com/stack/175442/maven-assembly-plugin-how-to-create-nested-assemblies.html
Thanks
I'm happy enough with the above result. However this is my first attempt at using Maven. If someone has a better way of doing this I'd gladly take the advice.

Include specific dependencies with maven assembly plugin

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

Maven assembly include snapshot depedencies

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>

How to create a release package with maven?

I build a desktop application using Maven2.
I'd like to make a release from time to time (just copy all the project's and third party jars into a single dir and generate a run.bat file).
How to do it ?
You need to create run.bat yourself and place it in src/main/assembly/scripts, for example. Then you need to create an assembly.xml file in src/main/assembly.
Here is an example of an assembly.xml file that you might want to use. It creates a tar.gz with all your dependency jars and your run.bat.
<assembly>
<id>1.0</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/assembly/scripts</directory>
<outputDirectory>/scripts</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Finally, in your pom.xml file add the assembly plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
Now, when you run "mvn install" you should see your tar.gz created.
To release run:
mvn release:prepare
mvn release:perform
OK I got it with a little help of dogbane's answer
I used my own assemlby file src/main/assemlby.assembly.xml
<assembly>
<id>teleinf</id>
<formats>
<format>dir</format>
</formats>
<moduleSets>
<moduleSet>
<includes>
<include>pl..........:core</include>
<include>pl..........:gui</include>
</includes>
<binaries>
<outputDirectory>../../release</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<fileSets>
<fileSet>
<directory>src/main/assembly/</directory>
<outputDirectory>../../release</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
and added following to pom
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
I had to write a run.bat myself - so it's not fully satisfying but will do.
i would go with the maven release plugin, see:
maven release plugin
using maven release plugin
Just an addition to the answer given by dogbane
Your .bat file will running packaged jar file with a filename reflecting the current version of the application, e.g.
java -Xms256m -Xmx350m -jar bin\yourApp-1.10.1-SNAPSHOT.jar
On every release this file will need to get updated with a new version name of the application. This can be automatized, too.
In your pom.xml file add this section:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.build.sourceDirectory}/../assembly/scripts</directory>
<includes>
<include>yourApp.bat</include>
</includes>
</resource>
...
</resources>
...
</build>
This asumes that you have put the yourApp.bat file in the folder:
src/main/assembly/scripts
Content of the yourApp.bat file should look like this:
java -Xms256m -Xmx350m -jar bin\${project.build.finalName}.jar
Just run the Maven commands and enjoy.

Categories