I have a Java project Foo, it has dependence on bar.jar and its maven build is as following:
-- foo/
---- foo.jar
---- lib/
------ bar.jar
------ (other lib jars)
The foo.jar's MANIFEST.MF has field Class-Path: . lib/bar.jar ... (other lib jars) and Main-Class: com.abc.foo.Main and it can be invoked by command java -jar foo.jar
The bar.jar is created from another Java project Bar which has dependence on httpclient-4.5.jar.
So the question is, when distributing the build of Foo which contains bar.jar in its subfolder lib already, do I need to include the httpclient-4.5.jar as well?
Update:
I figured out the real question I want to ask is about Transitive Dependencies. According to https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies, Maven is able to discover the lib(dependence) that your own lib requires.
In my case is that Maven can figure out foo depends on bar and bar depends on httpclient-4.5.jar, and it will grab the httpclient-4.5.jar to Project Foo's Maven Dependencies list.
This works well for most dependencies in my project, i.e. the dependencies' dependencies also appear in my Maven Dependencies. However, there is one dependency(bar.jar) that Maven does not retrieve its dependencies(httpclient-4.5.jar). That's the key issue I have right now.
You can use maven-assembly-plugin with it you can build a bundle zip for distribute you java project.
for example
in the pom.xml
<dependencies>
<!-- foo dependencies -->
<dependency>
<groupId>com.company.bar</groupId>
<artifactId>bar</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>foo</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Build-Time>${maven.build.timestamp}</Build-Time>
<Build-Host>${agent.name}</Build-Host>
<Build-User>${user.name}</Build-User>
<Build-Maven>Maven ${maven.version}</Build-Maven>
<Build-Java>${java.version}</Build-Java>
<Build-OS>${os.name}</Build-OS>
<Build-Label>${project.version}</Build-Label>
<Build-Path>${basedir}</Build-Path>
<Build-Number>${buildNumber}</Build-Number>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<id>distro-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/asembly/asembly.xml</descriptor>
</descriptors>
<!-- <finalName>${project.artifactId}-${project.version}.jar</finalName> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
you must give the specification of what it's in the bundle into the asembly.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>dist</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>./bin/</outputDirectory>
<includes>
<include>foo.jar</include>
</includes>
</fileSet>
<fileSet>
<includes>
<include>readme.txt</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
when you do mvn clean install then in the target forlder, must appear the bundle as foo-dist.zip
in the zip you will have in the /lib folder bar.jar and every other jar declared as dependency and in the forlder /bin foo.jar.
Related
I have 2 projects, project A and project B. Project A is dependent on project B.
pom.xml of project A:
<dependencies>
<dependency>
<groupId>nft.dcs</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-vision-plugin</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/dep.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Next, I compile my project and it creates a jar with all the dependencies that I don’t need. Created extra folders that do not have to be included in the jar file. I think these dependencies drag along with the dependency from project B.
Using the dep.xml file, I indicate which folders you do not need to include in the jar file.
dep.xml:
<id>with-spec</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>nft/**</exclude>
<exclude>com/**</exclude>
<exclude>impl/**</exclude>
<exclude>javax/**</exclude>
<exclude>junit/**</exclude>
<exclude>org/**</exclude>
<exclude>oshi/**</exclude>
<exclude>sound/**</exclude>
</excludes>
</unpackOptions>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
This method is just awful. Please tell me whether it is possible to add only classes from the dependency to the jar file and not to pull all these other folders.
You can try using scope provided:
<dependencies>
<dependency>
<groupId>nft.dcs</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
You should take into account that, as maven documentation states:
indicates you expect the JDK or a container to provide the dependency at runtime
You are creating a jar with dependencies, probably to run it standalone.
So if A needs B, and B has dependencies on its own, A probably needs those as well to run. It is therefore sensible to pack all the dependencies of B into the runnable jar.
If B has unnecessary dependencies, these should be removed from B.
If B has dependencies, that for some reason are not required when A uses B, you should add a proper exclusion to the dependency in the POM.
We're trying to build a client jar that includes unpacked dependent jar's. And the manifest should have class-path entries to the dependent jars. The snippet below works but the jars are unpacked - how can we stop the jars from being unpacked?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Indeed, assembling using jar-with-dependencies causes maven to unpack all the dependencies as ${assembly.dependencySets.dependency.unpack} is set to true in the corresponding assembly descriptor.
A simple fix would be to provide an assembly descriptor similar to the jar-with-dependencies.xml and modify ${assembly.dependencySets.dependency.unpack} to false, like this:
EDIT: For an unknown reason, the behavior when using <unpack>false</unpack> is not exactly the same and it seems necessary to add <outputDirectory>/</outputDirectory> to the fileSet or you don't get the expected result.
<assembly>
<id>uberjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
You can add your dependencies as jar files to your jar:
assembly-descriptor.xml
<assembly>
<id>uberjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
</fileSets>
</assembly>
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-uberjar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>src/main/assemble/assembly-descriptor.xml</descriptor>
</configuration>
</execution>
</executions>
</plugin>
But unfortunately you can't use the Class-Path header in manifest.mf, see Adding Classes to the JAR File's Classpath:
Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
The solution proposed by Pascal Thivent defines a new assembly for the Maven assembly plugin. The Maven assembly plugin offers defaults assemblies which are 'bin', 'jar-with-dependencies', 'project' and 'src' producing various predefined bundles.
A new assembly has to be defined in a new xml file, most of the time located in src/assemble. Then it will be loaded instead of the predefined one, this way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<!-- disabled predefined assembly -->
<!--
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
-->
<descriptors>
<descriptor>src/assemble/myAssembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
I'm currently moving a small project in to Maven and I am having some trouble.
The setup is as follows
Where a setting file is used in a my resource folder for running the application in eclipse. But when i create a fat jar the setting file is packed in side the jar. I want to filter the setting file so it is outside the jar (I understand this makes the file brittle, but that is by design) and still have it available to the application from the same folder as the jar. I have achieved this with an eclipse runnable jar and and Ant file. But I cannot seem to get it to work with maven. Any insight you have would be very useful.
Her are examples of my plugin and filter file. Any Ideas on this would be great as this has been driving me insane
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<filters>
<filter>src/assembly/filter.properties</filter>
</filters>
<archive>
<manifest>
<mainClass>ie.business.project.artifact</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
and my filter
<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>distribution</id>
<formats>
<format>jar</format>
</formats>
<files>
<file>
<source>settings.ini</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
So I have tired something else. If I define an exclusion in my jar-plugin in addition to using maven-assembly, the setting file dose not end up in the fat jar. I'm unsure if this is the right way to do this but it has worked.
Please let me know if there in a better way to do this.
I added the following plugin to my pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>setting.ini</exclude>
</excludes>
</configuration>
</plugin>
It seems that you are not using the assembly plugin pointing to the assembly in your question. If you look at the plugin configuration you are using:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
Actually you should be pointing to the relative path of the assembly file, like e.g:
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
Now the assembly file for the creation of a fat jar should be changed to create the fat jar and exclude some files. This example is just to give you an idea:
<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>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<excludes>
<exclude>settings.ini</exclude>
</excludes>
</fileSet>
</fileSets>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
Here's the plugin for maven WAR plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
What plugin can generate a ZIP file containing the same files in the WAR?
A possible solution is to create a custom assembly with the use of the maven-assembly-plugin.
The following descriptor declares an assembly of format zip. It uses a <dependencySet> to declare the dependency towards the WAR packaged by the Maven build. This WAR is unpacked so that the final ZIP has the exact same content has the WAR file.
<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>zip</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>${project.groupId}:${project.artifactId}:war:${project.version}</include>
</includes>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
Save this file, for example in src/assembly/assembly.xml. Then, in your POM, you need to declare an execution of this plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
When you invoke Maven with mvn clean install, this plugin will be executed and it will create the wanted ZIP file inside the target folder.
I know that it seems like a question that is already asked, but I have not found an answer that helps me a lot.
I read a lot of times that it is preferable to generate an artifact per project. But my question is : what can I have as a Maven artifact?
For example : I have a custom packaging "MyPack" with a custom lifecycle, can I have whole a directory "Mydirectory" as an artefact?
Let's say that the structure of the directory is like this:
MyDirectory
-----|--jars
-----|-----|--- client.jar
-----|-----|--- server.jar
-----|--jsps
-----|-----|--- page1.jsp
-----|-----|--- page2.jsp
-----|--imgs
-----|-----|--- img1.png
-----|-----|--- img2.png
Then, I want to create a new project that has the packaging "MyPack" and a dependency with the type "MyPack". In this project, my Java classes need client.jar and server.jar which are in the Maven repository to compile and I want to copy all jsps and imgs from the repository in my new project.
can I do all that with a custom Maven plugin?
You can have as maven artifact whatever you choose to have as an artifact.
Use the maven assembly plugin to generate the artifact:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/assembly/shared.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
....
</plugins>
....
</build>
In the shared.xml file you specify the format and files to be included in the artifact:
<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>shared</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>${basedir}/.....</directory>
<includes>
<include> ... </include>
</includes>
<fileMode>0644</fileMode>
<filtered>false</filtered>
<lineEnding>unix</lineEnding>
</fileSet>
</fileSets>
</assembly>
When you do "maven deploy", the "shared" artifact will be packaged and deployed together with the project's main artifact.
In order to use the artifact in another project, use the maven-dependency-plugin with the goal "unpack-dependencies".