I am using maven assembly to build my java application and package it as a JAR file. The resources are located in src/main/resources and they are not copied to the created JAR file.
The POM.XML is like this:
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.test.Application</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Please let me know how can I fix this! I tried to use Resource tag, but it did not work.
Related
I had added annotation processor in my pom.xml file, it worked properly to generated-resources folder with using terminal outside eclipse but with the terminal inside eclipse, the generated-resources folder sometimes was empty. How can I solve this? Thanks in advance!
<build>
<finalName>${project.artifactId}</finalName>
<directory>${buildDirectory}</directory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessors>
<annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
<annotationProcessor>com.zenos.core.annotation.processing.PropertyNameAnnotationProcessor</annotationProcessor>
<annotationProcessor>com.zenos.core.annotation.processing.JsonSchemaAnnotationProcessor</annotationProcessor>
</annotationProcessors>
<compilerArguments>
<AaddSuppressWarningsAnnotation>true</AaddSuppressWarningsAnnotation>
<AjsonSchemaSrc>src/main/resources/com/zenos/json/schema</AjsonSchemaSrc>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<ejbVersion>3.0</ejbVersion>
<archive>
<manifestEntries>
<Version>${maven.build.timestamp}</Version>
<Dependencies>deployment.zenos-app-1.0-SNAPSHOT.ear.zenos-ejb.jar, org.infinispan services export</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<versionRange>[3.1.0,)</versionRange>
<goals>
<goal>jar</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
I have a maven project in Netbeans.
Running it shows a GUI in which you can select and read files using a JFileChooser.
The file chooser has the method getSelectedFiles() which returns a File[] which then I feed into fileProccesing(File[] files) to read them using PDDocument.load(file) from Apache pdfBox depedency.
Debugging has led to the conclusion bellow.
When running in Netbeans it works fine but when it's running as a jar executable it stops at the PDDocument.load(file) command. However no IOException is thrown.
Ether JFileChooser getSelectedFiles() returns some abnormal type of files or my pom.xml is problematic in some way. You can see my pom.xml bellow.
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>application</groupId>
<artifactId>toolToManagePdfs</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>toolToManagePdfs</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.9.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>
application.App
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
A normal JAR does not contain or reference its dependencies at runtime. If you want to run a JAR, you need to either
build an executable JAR with the maven assembly plugin or maven shade plugin.
list the classpath explictly when running the JAR.
Here's what I use to have the jar files in the lib subdir:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>application.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
The problem stemmed from having all plugins inside <pluginManagement></pluginManagement>.
Moreover the configuration was incomplete and didn't include the dependencies inside the jar.Using maven-assembly-plugin fixed that.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
application.App
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
I've got a Java/Selenium/TestNG/Maven project. I can successfully create the myproj-0.0.1-SNAPSHOT.jar file. When I try to run it I'm getting an error:
$ java -jar myproj-0.0.1-SNAPSHOT.jar
Error: Could not find or load main class utilities.MavenTestInvoker
My main goal is to be able to run this SNAPSHOT.jar file to run my test cases on a remote machine.
I'm using a build plugin in my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>utilities.MavenTestInvoker</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
When I build the project I'm using the maven command: "mvn clean package shade:shade"
That creates my uber jar.
Here are the dependencies:
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</dependency>
plus some others
</dependencies>
What am I doing incorrectly?
The correct usage of including plugin dependencies is as follows, you can merge the plugin dependencies at a single place -
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<!--Notice I have removed the configs here. Not required to generate the jar-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Further you can edit the respective plugin configs within the same declaration.
Also for using maven-shade-plugin and maven-jar-plugin, you might want to look into the samples here and here respectively.
Remember that your project must have the right folder structure, for example your code must be in src/main/java
I have a parent pom.xml that defines the emma-maven-plugin with inherited=true.
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven.site.version}</version>
</plugin>
<plugin>
<groupId>org.sonatype.maven.plugin</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>${emma.maven.version}</version>
<inherited>true</inherited>
</plugin>
</plugins>
</reporting>
However I have a child pom.xml that does not contain any tests. So I tried adding this... execution none, inherited false and skip true to skip the emma report generation, but it does not seem to work. Any ideas?
<build>
<plugins>
<plugin>
<groupId>org.sonatype.maven.plugin</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>${emma.maven.version}</version>
<inherited>false</inherited>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.sonatype.maven.plugin</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>${emma.maven.version}</version>
<inherited>false</inherited>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</reporting>
I found a way to not instrument certain package and thus not creating coverage.em thus not creating EMMA report.
<build>
<plugins>
<plugin>
<groupId>org.sonatype.maven.plugin</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>${emma.maven.version}</version>
<configuration>
<filters>
<filter>-com.package.*</filter>
</filters>
</configuration>
</plugin>
</plugins>
</build>
I am trying to add SVN revision number in the manifest of my projects. To do so, I used Maven build number plugin and added the following lines in my Super POM:
<!-- Gets the SVN revision number -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
</plugin>
<!-- Add the SVN revision number in the manifest (works on Hudson only) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
Note the configuration:
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
I did that on purpose because if I have local modifications "doCheck" will prevent me from compiling (and I want to compile and test BEFORE commiting my work).
The "doUpdate" is also a problem for me as I don't necessarily want to update the code from repository. Same reason than above, I want to test locally before commiting (and potentially solving conflicts).
My problem is that in the manifest, what appears is:
Implementation-Build: ${buildNumber}
Thus the variable is not interpreted. What did I miss?
Thanks
Edit:
The problem is in fact with maven-bundle-plugin. I use it in my projets to generate OSGi bundles.
The POM packaging of the projects is thus:
<packaging>bundle</packaging>
Instead of:
<packaging>jar</packaging>
I guess this messes with the Maven lifecycle.
When I remove the maven-bundle-plugin everything works fine. But I cannot remove it as my applications are OSGi apps.
The problem was mixing maven-bundle-plugin and maven-jar-plugin to manipulate the Jar MANIFEST.
The solution:
In the Super POM:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Implementation-Build>${buildNumber}</Implementation-Build>
</instructions>
</configuration>
</plugin>
...
</plugins>
</pluginManagement>
<plugins>
<!-- Gets the SVN revision number -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
-</plugin>
</plugins>
</build>
And that's it!
Instead of using configuration/archive try using configuration/instructions to add your ${buildNumber}, like this:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Implementation-Build>${buildNumber}</Implementation-Build>
</instructions>
</configuration>
</plugin>
With your approach and Maven 2, it all worked. We switched to Maven 3, the maven-bundle-plugin was not happy (same behavior as yours). This approach did the trick.
You can make use of Maven Plugin Management.
Add your plugin definitions in the super pom under <pluginManagement/> like so:
<project>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
</plugin>
<!-- Add the SVN revision number in the manifest (works on Hudson only) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
...
</project>
And then in the project where you want to use these now configured plugins you add this to the poms:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
...
</project>
That should sort it out for you.