Packaging specific dependent jars with Maven - maven

My maven project has several dependent jars, but when I create a jar of my project I would like to include a subset of those dependencies. Is there a way to do this? Currently, I am using the pom.xml below (from question How can I create an executable JAR with dependencies using Maven?), but it is packaging every dependency with my project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Try:
<dependencySets>
<dependencySet>
....
<excludes>
<exclude>commons-lang:commons-lang</exclude>
<exclude>log4j:log4j</exclude>
</excludes>
</dependencySet>
....
</dependencySets>
See Including and Excluding Artifacts for more details

Related

Not able to add resources using Maven jar plugin

I have a src folder out of which i need to create multiple jars. Like each package as a separate jar. Also i need to put specific xml files from src folder into specific jars. I created a pom which creates multiple jars successfully using multiple executions in plugin. But the problem is the movement of xml files to jars is not happening. Have provided the pom file below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>tra_common_id</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<finalName>tra_common</finalName>
<encoding>ISO-8859-1</encoding>
<classifier>tra_common</classifier>
<includes>
<include>com/**/common/**</include>
<include>com/**/apm/*Access*</include>
</includes>
<excludes>
<exclude>com/**/management/**</exclude>
</excludes>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</execution>
<execution>
<id>tra_client_id</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<finalName>tra_client</finalName>
<encoding>ISO-8859-1</encoding>
<classifier>tra_client</classifier>
<includes>
<include>com/**/client/**</include>
</includes>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</execution>
</executions>
</plugin>

Netbeans 8.0.2 Maven Dependencies

I have a Netbeans 8.0.2 Java project using Maven. I wish to produce a JAR with all dependencies included. I have seen similar questions but none of which seem to have a the solution that said I am new to it.
This is what i have added to the pom.xml (excluding dependencies etc.) from what I have read online but still no luck.
<properties>
<mainClass>com.oss.nia.startup</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Copy dependencies with maven

I want to copy dependencies to target/lib directory.How can i do this by maven?
I tried this:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.aproject.App</mainClass>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
But it copy libraries inside jar(i want to directory).
I use exactly the same as you posted above, but I'm using version 2.8 of the maven-dependency-plugin. Have you tried updating the plugin dependency?

How to include custom files in JAR with Maven assembly plugin jar-with-dependencies

I need to include custom file (/com/app/log4.properties) in a final JAR.
How can I add one file to my JAR when using jar-with-dependencies?
Now there are only class files in that JAR. I'm using:
mvn assembly:assembly
My pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.app.Start</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>
</plugins>
</build>
Thank you.
Put non-Java files that need to be in the final artifact in src/main/resources, in this case:
src/main/resources/com/app/log4j.properties

No manifest file from maven build

I'm using Maven to generate a war file. I'm trying to get it to generate a manifest file in the war. Right now it's not happening. I've included the following in my pom.xml, but I can't get it to output a manifest file with that information. Anyone have any ideas or pointers? There is no MANIFEST.mf being put into the war.
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
...
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>
You should add this to your plugin config:
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>

Resources