buildnumber-maven-plugin does not resolve its ${buildNumber} property - maven

My trouble is here:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${buildNumber}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
What I'd like to do is to get the build number in runtime through manifest properties. There are plenty of instructions on how to use buildnumber-maven-plugin but I don't have a clue why in my manifest ${buildNumber} variable is not resolved.
<scm> tag is present in POM.
When building I see the following message:
Storing buildNumber: a391951e51a593c5267a77264938d982696ed65a at timestamp: 1426368059203
Still here is the result:
Implementation-Version: ${buildNumber}
Any help is much appreciated. Thank you.

Related

mvn output jar file is executable only inside target folder

I have a mvn built jar, which is working fine inside the target folder.
But if I copy the jar to different folder/machine.I get a class not found exception. I understand the jar is unable to access the dependencies. how can i fix this?
my pom contains,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.outputDirectory}/dependency/
</outputDirectory>
</configuration>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.Main</mainClass>
<classpathPrefix>dependency/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Thanks In Advance,
Saranya
Make a fat jar with dependencies using maven-assembly-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The target folder would have additional jar-with-dependencies.jar; you should be able to use this anywhere.
I removed the maven jar and dependency plugins from my pom. Added
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.importer.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>xyz</finalName>
<appendAssemblyId>false</appendAssemblyId>
</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>
This created a jar with dependencies which can be executed anywhere. Also with the specific jar name.

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?

maven build jar with test resources file only

I have a project which will be built has jar.
structure of project as below
> src | - main
> |- java
> - resources
> | - test
> | - resources
so now i want to create 2 jar files using maven
1) main/java and main/resources
2) main/java and test/resources
My POM
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.tool.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You never specified what is happening for you that is not what you're after, but looking at your example, it looks like you've got two different versions of the same plugin defined in your POM to try and hack what you want.
You can do what you're after by using the <execution> tag to define the desired behavior.
<plugin>
<!-- jar plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<!-- use these to define different execution tasks during the package phase -->
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${project.mainClass}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>test-jar</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix> <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
After running this, you should have two jar's in your output directory, one named [your artifactID]-[version].jar and one named [your artifactID]-[version]-tests.jar

Maven archiver uses unlocked snapshots in classpath, but copy-dependencies copy locked snapshots

I am trying to use the maven-jar-plugin and maven-dependency-plugin to create an runnable "bundle" of my application. It works fine in most cases, but when I have a snapshot in the dependency hierarchy, the copy-dependencies goals seems to translate the snapshot-dependencies into locked snapshots (snapshots with timestamp)
However, addClasspath from archiver-plugin does not translate snapshot dependencies:
in lib, there is foolib-1.0.1-20130108.143522-8.jar
the classpath contains lib/foolib-1.0.1-SNAPSHOT.jar
so I can't run the application.
I can't find a way to tell the copy-dependencies to not translate SNAPSHOTs or one to tell the archiver-plugin to translate SNAPSHOTs.
Here is the relevant snippet of the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-libs</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeScope>provided</excludeScope>
<outputDirectory>${package.dest}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${package.dest}</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
A new option (useBaseVersion) in maven-dependency-plugin 2.6 can fix this. So you need at least version 2.6.
Note: I needed the useBaseVersion option set to false, as my problem seems to be the opposite of the original question. So the original version probably requires useBaseVersion is set to true, which is the default value.
Below is an example on how to change the version number and set useBaseVersion to false in the pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!-- need at least 2.6 for useBaseVersion-->
<version>2.6</version>
<executions>
<execution>
<id>copy-libs</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeScope>provided</excludeScope>
<outputDirectory>${package.dest}/lib</outputDirectory>
<!-- useBaseVersion=false makes the jar names match those
that maven-jar-plugin puts in the manifest classpath -->
<useBaseVersion>false</useBaseVersion>
</configuration>
</execution>
</executions>
</plugin>
The other alternative is described here: https://stackoverflow.com/a/45642898/232175
Setting useUniqueVersions to false for the maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- ... -->
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${main.class}</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>

Resources