Not able to add resources using Maven jar plugin - maven

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>

Related

Maven: File does not get excluded from build

I have a library maven module which has an application.conf file in /src/main/resources/ and I want to exclude it from the build in the pom of the module
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.conf</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
A maven module, which uses the above mentioned module, shall build a jar with all dependencies; I use the maven assembly plugin to build actually the jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<!--<descriptor>src/assembly.xml</descriptor>-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The file application.conf is still there in the jar. What I am doing wrong here ?
do this :
<exclude>**/application.conf</exclude>

Maven: properties-file alongside jar

I am packaging my application as jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
And also I have my .properties-file placed inside of src/main/resources. I can freely move this file to any other place. I don't want properties-file to be included into jar, but rather placed into the same output directory (where we get jar) side-by-side. What is the best practice for this in Maven?
Okay, one can use goal resources:copy-resources of maven-resources plugin.
I am just leaving standard resources folder for embedded (into jar) resources. And for external resource I create another folder: src/main/external-resources. Here I put my .properties-file.
Following piece will copy external resources to output dir upon validate phase. You can alter the phase per your own needs.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/external-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Packaging specific dependent jars with 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

can maven assembly plugin picking the specify java package?

Suppose we have following packages in our project
org.xxx.dao, org.xxx.service, org.xxx.service.impl
all the package are in ther source folder src/main/java
can i assembly packages into seprated jars,(xxx-dao.jar,xxx-service.jar,xxx-service-impl.jar)?
You can use the standard maven-jar-plugin for this:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>dao</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>dao</classifier>
<includes>
<include>org/xxx/dao/**/*</include>
</includes>
</configuration>
</execution>
<execution>
<id>service</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>service</classifier>
<includes>
<include>org/xxx/service/**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
This gives you one JAR with everything and then several JARs with only the selected content which you can select with a classifier in your Maven POMs.
If you want to omit the default JAR (which contains everything), then you replace one <id> with <id>default-jar</id> and add an <excludes> element because the default JAR includes everything, so you have to get rid of anything that you don't want.
maven-assembly plugin is not intended for this kind of tasks, see its features. If you have the source codes, then I would suggest copying them into a module of your project.
But if you want to do it with maven-assembly plugin, then I think you can do it by creating separate descriptor components and defining which files to include in which assembly. You can define source files to be included in includes tags under sources definition.

Unable to get unpack-dependencies to run before jar:jar,

I cannot get maven-dependency-plugin/unpack-dependencies to run before maven-jar-plugin/jar when running mvn clean install from the command line.
Every time, I see it running jar:jar before the unpack stuff runs, I saw in my googling some talk of adding a pre-package phase to the maven lifecycle, doesn't seem to be working thought.
Basically I want to create a single jar file containing all necessary classes (all 2600 of them). This jar gets a Manifest which enables it to be run in the manner of:
java -jar blah.jar
If I ever get it to work...
And here's the xml snippet...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>archivedb.Read</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
In your original question, replace the phase with the following phase.
<phase>prepare-package</phase>
this will cause the jars to be extracted first. For your problem, the shade solution is better, but I will still post this here as a reference for other with similar problems where shade does not help.
The output path is set to make sure that jar's contents end up in dir that the jar:jar goal packages (target/classes).
Full plugin execution xml snippet:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includes>**/*.class</includes>
<excludes>**/*.properties</excludes>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I had similiar issue with single jar creation.
My solutions incorporates standard maven-dependency plugin with unpack goal (unpacks all dependencies during compile process and move it into custom outputDirectory.
The secret is, JAR plugin takes all from target/classes directory and packs as a one JAR.
That's why I defined custom outputDirectory. Have a look at the code below:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<archive>
<manifest>
<mainClass>pl.company.client.Uploader</mainClass>
</manifest>
</archive>
</configuration>
<version>2.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
<version>2.3.2</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>compile</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
You may run it by identifying the goal sequentially as
"mvn clean dependency:unpack jar:jar install"
I hope this may help you to achieve the requirement
Regards,
Charlee Ch.

Resources