Maven Assembly Plugin : Resources are copied at root of jar - java

I'm using the maven-assembly-plugin to build a Java 8 project, and package it as a JAR file.
I've got several resources located at src/main/resources (usual maven resources directory).
When the JAR is created, all resources files under src/main/resources are copied at the root of the JAR.
In the code, I'm trying to open files using FileInputStream that are located in src/main/resources, and it works great when running the project without packaging it as a JAR file. However, when I'm running the project from the JAR file, I get many FileNotFoundException because my resources are not anymore at src/main/resources, but at ${project.basedir} instead.
I'd like to have them located at src/main/resources in the JAR file. Is it possible?
My build configuration is as follow:
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>EmployProject.TestAlgo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>TestAlgo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
Edit
If it's not possible, what, in my code, should I use to access my resources files?

Thanks to #khmarbaise and this post: Accessing a Java Resource as a File
I figured out I was using the wrong API to open resources.
Resources in a JAR are not files, thus I changed:
XMLDecoder xmlDec = new XMLDecoder(new InputFileStream(this.getClass().getResourceAsStream(fileName))
to:
InputStream in = EnumTools.class.getResourceAsStream(fileName);
XMLDecoder decoder = new XMLDecoder(in);

Related

Making absolute reference to a classpath in Maven

I have a customized jar that the application uses. Instead of installing jar in maven I want to make reference to it by defining absolute location for that jar in the manifest file. I am using the code shown below to update information in manifest file. However, I am not sure how can I reference to the jar location that uses absolute path like : D:/Shared_library/Test.jar . This might not be a good practice but I want to see if there is anything like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
You can add it using <manifestEntries> tag.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>file:///D:/Shared_library/Test.jar</Class-Path>
</manifestEntries>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Maven copy resources

In the resources in the profile folder there is a configuration file, depending on the profile it should be taken and put in the resource root
Actually, what I encountered
He puts it in the project itself, and not in the original jar
When the second time you collect this file already comes
So that the folder itself in the jar does not fall
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<executions>
<execution>
<id>copy-properties</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources/profile/${profile.dir}</directory>
<includes>
<include>server.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>${maven-ejb-plugin.version}</version>
<configuration>
<filterDeploymentDescriptor>true</filterDeploymentDescriptor>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<ejbVersion>3.0</ejbVersion>
</configuration>
</plugin>
</plugins>
Tell me where I was wrong?
In which plugin to configure maven-ejb-plugin or maven-resources-plugin
I have src/main/recoursces/profile/
serverA/server.properties
serverB/server.properties
serverC/server.properties
I want jar
- It did not have a folder profile
- and have one server.properties
I'm not sure if I understand your question. I believe the resources phase package is not correct, it should be on a previous one like process-resources.
Here you have the Maven Lifecycle Reference
process-resources: copy and process the resources into the destination directory, ready for packaging.
package: take the compiled code and package it in its distributable format, such as a JAR.

How to package a library in to a .jar file?

I want to put jar into my generated jar file .
i want to include a new dependency (external.jar) for my executable jar file.the external.jar do not contain a main method.
Use jar cf jar-file input-file(s). I recommend reading http://www.skylit.com/javamethods/faqs/createjar.html , https://docs.oracle.com/javase/tutorial/deployment/jar/build.html and Java creating .jar file
If you are using mavan, you can add the next to pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

How to format the name of a JAR File, which will be created with Maven3?

I have a question concerning Maven3. I want to create a JAR File with the name myJarFile_1.0.0-SNAPSHOT_200140723.jar.
But Maven only create the follwoing name (I used the command "mvn clean install"):
myJarFile_1.0.0-SNAPSHOT_200140723-jar-with-dependencies.jar
Here is my finalName declaration:
<finalName>${project.artifactId}_${project.version}_${timestamp}</finalName>
Here is the configuration of my Maven-asembly-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifestEntries>
<source-jdk>1.6</source-jdk>
<target-jdk>1.6</target-jdk>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>myMainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assembly-jar-Id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
How can i solve this problem?
I can only work with the JAR File, which includes the text "jar-with dependencies". But i must rename this file, because of my bash sell script, which execute this File. The second JAR file, which was created by maven, can not be used, since i get this error message: no main manifest attribute, in myJarFile.jar
Many thanks !

Maven: Not copying files from src/main/resources to the classpath root - Executable Jar

I am building an executable jar with Maven using the Assembly plugin. I have a resource file(.xml) which is placed at src/main/resources. When I build the executable jar, the file is not getting copied into the jar - Checked by unpacking the jar.
Here is my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
I am trying to call the following resource which is kept under src/main/resources:
reader = Resources.getResourceAsReader("mybatis-configuration.xml");
Getting the following exception while executing java -jar InterfaceRunner.jar
Exception caught while reading or parsing the mybatis config xml :java.io.IOException: Could not find resource mybatis-configuration.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:108)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:95)
at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:153)
Has anyone faced a similar issue before? Looking for your help, Maven gurus..
You can try replacing your Configuration as follows;
<configuration>
<archive>
<manifest>
<mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>InterfaceRunner</finalName>
</configuration>
And then
mvn package
By default, maven-assembly-plugin makes TWO jars, not one (with phase package). Make sure you are running the one called <artifactid>-<version>-jar-with-dependencies.jar
If this is not the problem, are you doing anything weird in your pom here:
<build>
<resources>
<resource>
<targetPath><!-- here?? --></targetPath>
</resource>
</resources>
<!-- etc. -->
</build>

Categories