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>
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 copy some files (jar, launch scripts, docs) to some directory, like dist/ in project root.
I am using maven-assembly-plugin and set <configuration><outputDirectory> in pom.xml. It creates files in dist/ but inside <my_project>-<decsriptor_id>/ subdirectory.
Is there any way to output it just in the root of dist/?
Or is there a plugin in Maven that simply copies files?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>maven-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}/dist</outputDirectory>
<descriptors>
<descriptor>${project.basedir}/src/main/maven-assembly/dist.xml</descriptor>
</descriptors>
</configuration>
</plugin>
dist.xml
<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>dist</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>path........</source>
<fileMode>0755</fileMode>
<outputDirectory>.</outputDirectory>
</file>
</files>
</assembly>
You may use maven-resources-plugin:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- insert here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Following this Guide I ran the command
mvn assembly:assembly
and got the Build Failure of
Error reading assemblies: No assembly descriptors found.
I've looked at numerous questions on this, but to no avail.
From this post, I created a .xml with this inside:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>**/LICENSE*</exclude>
<exclude>**/README*</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources/META-INF/services</directory>
<outputDirectory>META-INF/services</outputDirectory>
</fileSet>
</fileSets>
</assembly>
and included this in the pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>jar-with-dependencies.xml</descriptor>
</descriptors>
</configuration>
</plugin>
but still no luck.
I'm pretty new to this as you can probably tell, how can I get this running?
~~EDIT~~
In the pom.xml I changed
<descriptor>jar-with-dependencies.xml</descriptor>
To
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
~~EDIT 2~~
pom.xml now contains this:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
~~EDIT 3~~
This pom.xml now works for me:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
For this to work, you need to create the file jar-with-dependencies.xml in src/main/assembly/ and this XML:
<descriptors>
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
</descriptors>
i.e. you need to specify the path to the file and the convention is to put the files into src/main/assembly/.
To use the ones provided by Maven, you need to use the descriptorRef element instead (wrapped in a descriptorRefs).
Also don't put the descriptor inside of the execution element or mvn assembly:assembly can't find it anymore (since you specifically moved it to the mvn package target).
[EDIT] I followed the tutorial myself and there is an important point which you might have missed: You need to select the correct archetype. In my case, that was 5 but the order can change. So read the whole list and look for the string openimaj-quickstart-archetype or things will break.
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
I have a desktop Java application built using Maven 2 (but I could upgrade to Maven 3 if that helps) with a number of open source dependencies. I'm now trying to package it up as a standalone to make it available to end users without them needing to install maven or anything else.
I've successfully used maven-assembly-plugin to build a single jar containing all dependencies but this is not really what I want because when using LGPL libraries you are meant to redistribute the libraries you are using as separate jars.
I want Maven to build a zip containing a jar with my code and a MANIFEST.MF that refers to the other jars I need together with the other jars. This seems like standard practice but I cannot see how to do it.
Here's an extract from my pom
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<compilerVersion>1.6</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.company.widget.Main</mainClass>
<packageName>com.company.widget</packageName>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.company.widget.Main</mainClass>
<packageName>com.company.widget</packageName>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
EDIT:Taken on Kals idea
created a file called descriptor.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>distribution</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
and pom contains:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.company.widget.cmdline.Main</mainClass>
<packageName>com.company.widget</packageName>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now maintains the jar and puts them all in the lib folder, including my own code
You should check out the Maven Appassembler plugin. You can get a lot more robust package using it than rolling your own assembly.
It generates helpful startup scripts for Unix and Windows that allow you to set predefined JAVA VM options, commandline parameters and classpath.
It also has a concept of configuration directory where you can copy default configurations that user can later change. You can also set the configuration directory to be available on the classpath.
Dependencies can be saved in a Maven style "repo" or you can use a flat style "lib" directory.
You still need the assembly plugin for creating a zip or tar archive.
Here's an example appassembler configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<programs>
<program>
<mainClass>com.mytools.ReportTool</mainClass>
<name>ReportTool</name>
</program>
</programs>
<assembleDirectory>${project.build.directory}/ReportTool</assembleDirectory>
<repositoryName>lib</repositoryName>
<repositoryLayout>flat</repositoryLayout>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
To get a zip archive I use the this assembly:
<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>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/ReportTool</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Try creating a custom assembly descriptor and add a dependencySet and make sure you specify, unpack as false.
Use this as assembly descriptor,
<?xml version="1.0" encoding="UTF-8"?>
<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>distribution</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
Store this file to say src/main/assembly/assembly.xml and update your assembly plugin configuration in pom.xml like this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
</configuration>
</execution>
</executions>
</plugin>
Here is assembly descriptor reference if you need anything more
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
As far as I know it is possible to redistribute also LGPL libraries into your own packages as long as they are untouched. The maven assembly plugin creates a jar that contains the original jars inside a lib folder of the archive. So far you are fulfilling the LGPL.
Maybe this question gives some more information about this topic.
(Disclaimer: I'm not a lawyer, so please crosscheck this information ;) )
Add the following plugins in pom.xml. Check the value at mainClass,classpathPrefix,addClasspath tags.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>org.apache.camel.spring.Main</mainClass>
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>src/assembly/some-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Create some-assembly.xml under src/assembly as below.
<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>distribution</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
Note that useProjectArtifact flag to false, unpack flag to false. If root folder inside zip file is not required,then one can make includeBaseDirectory to false.
This will create name-version-distribution.zip file. Inside zip file, there will be folder name-version. Inside this folder, your executable jar and lib folder containing all dependency jars will be present.
Check manifest.MF file of executable jar. It contains both main class and classpath information.