I have a simple Hello World application 'App' which has been made in a Maven project. I'm aware that after building the project, the Maven assembly plugin is required in order to execute the JAR which is created when the project is built. I have followed the instructions found at:
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
But after editing my pom.xml and re-building the project, I still cannot run my JAR file.
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mini-project-6-ex6</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.mycompany.mini.project.ex6.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Is there a mistake in my POM or additional lines which I have left out which is causing me to not be able to run the resulting JAR file?
EDIT: It seems as though the JAR has no manifest attribute (see comments). I suppose that I could open the jar using 7zip or similar program and manually add a manifest file, but the real question is how to create the JAR using Maven to include a manifest file in the first place.
You're looking at the wrong JAR. The POM above will create at least two JARs:
mini-project-6-ex6-1.0-SNAPSHOT.jar
mini-project-6-ex6-1.0-SNAPSHOT-jar-with-dependencies.jar
The former is only the code of the module, the latter is the code plus all the dependencies.
If you run mvn install, you will only get the first one since the Assembly Plugin isn't configured to run automatically. You can run it manually with mvn assembly:assembly.
If you want it to run automatically when you mvn install, you need to change the POM:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<configuration>
...
</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>
See http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly
Related
I'm struggling to execute a jar file that I'm creating. This is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject3</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.mavenproject3.SSBStub</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.17.0</version>
</dependency>
</dependencies>
</project>
When I try to run this jar file on command prompt, I get an error saying "Could not find or load main class". I've tried everything and can't seem to resolve it. Can someone please help me with this?
And also, can someone show me where I can find the manifest file too? I don't know where to look for it in netbeans.
Thanks
If your are trying to run it using the command prompt you would want all dependencies to be present in your jar itself.
You can use the maven-assembly-plugin for this. Use the below code to achieve this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.mycompany.mavenproject3.SSBStub</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Run with maven goals : mvn clean package
This should create two jars in your target directory. One without the dependencies and one with all dependencies. Run the jar named: myJar-jar-with-dependencies.jar
I created a program that writes to firebase and the program runs fine when I run it in the Netbeans IDE but when I build it to a .JAR file and run said jar file I get this message:
Heres my pom XML if that helps:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.FUJCode</groupId>
<artifactId>climate2.0</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<name>UsageMoniter</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Climate</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Im fairly sure I got all the dependencies so i don't know what could be wrong.
If you need anymore information let me know and any help would be greatly appreciated.
Many thanks,
Tom
EDIT
EDIT 2
here in the project folder it shows all the firebase stuff in the dependencies folder:
But when you look at the files there is no firebase business to be seen, so im not sure what to say...
You need to copy the necessary third party artifacts into your project, e.g. by extending your pom.xml:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<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>
...
</plugins>
</build>
Then you need to add your third party dependencies to the classpath of your application when starting it, e.g:
java.exe -cp "climate2.0-1.0-SNAPSHOT.jar;lib/*" -jar climate2.0-1.0-SNAPSHOT.jar
or create a uberjar with the help of maven hade plugin, see here
This will create another jar which contains all your dependencies when you build your project.
I know, it was written here many times, but I tried solutions, but with no success. I have netbeans maven project with dependencies. I want to make executable jar file with dependencies. This is my pom file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sitgaStatistics</groupId>
<artifactId>StigaStatistics</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<mainClass>stigastatistics.stigastatistics.StatisticsGUI</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>gov.nih.imagej</groupId>
<artifactId>imagej</artifactId>
<version>1.42</version>
</dependency>
</dependencies>
<name>StigaStatisticsGUI</name>
</project>
when I clean and build projext it will create 2 jar files in target direcotry (StigaStatistics-1.0-SNAPSHOT.jar, StigaStatistics-1.0-SNAPSHOT-jar-with-dependencies.jar) when I start them from terminal like:
java -jar StigaStatistics-1.0-SNAPSHOT-jar-with-dependencies.jar
it will respond me error:
Error: Could not find or load main class stigastatistics.stigastatistics.StatisticsGUI
I really don't know what is wrong with this.
Thanks for answers
The problem is the misspelled fully-qualified name of the main class. There is a typo in the first part of the package name.
Either rename the package sitgastatistics.stigastatistics or correct the <mainClass>stigastatistics.stigastatistics.StatisticsGUI</mainClass> property definition so that the package name reads the same.
I'm currently working with a Legacy app, whose process for building plugins requires adding dependencies on about 150 non-maven (ant I believe?) jar files. Ideally, I'd like to package these 150-jars into a single JAR, place it on Artifactory, and use that as a maven-dependency so that my team can more easily setup their development environment.
I've been experimenting with one-jar, maven-assembly-plugin, and maven-shade-plugin, which appears to just create one jar that contains several other jars (i.e. unzip the contents). However when adding that jar as a maven dependency, maven appears unable to resolve the contents/dependencies within these "sub-jars."
The code below is only an example of something I've tried; so feel free to suggest other approaches.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>some-jars</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>someName</name>
<properties>
<jdk.version>1.8</jdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.mypackage.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<binlibs>
<fileSet>
<directory>${basedir}/jars</directory>
<includes>
<include>*</include>
</includes>
</fileSet>
</binlibs>
<!-- Optional, default is false -->
<attachToBuild>true</attachToBuild>
<!-- Optional, default is "onejar" -->
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
</project>
Perhaps you may want to consider the BOM approach adopted by Spring IO Platform (http://platform.spring.io/platform/#quick-start), JBoss Java EE 6 spec dependency in Maven (http://www.andygibson.net/blog/quickbyte/jboss-java-ee-6-spec-dependency-in-maven/)
By using BOM approach, there is no need to bundle all the libraries to one single jar.
I had the same problem a while ago
For this reason i implemented maven-bulk-deploy that make easier to implement the BOM approach
Just you have put your jars in one folder choose a common-group-id/version (eg. the same of your main project) run the plugin and it will create a BOM file (ie pom ) and will deploy all jars in your asset repo
Feel free to checkout/fork the project to try it for solve your issue
My situation is:
I have a project that scan a directory of libs.
This project dosen't has dependence with other projects (jars), but this dynamically load them from the directory to his classloader (is a kind of plugin architecture).
Both projects are under a pom project
Examples of poms:
Parent:
<groupId>mygroup</groupId>
<artifactId>myparent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
...
"Plugin":
<parent>
<artifactId>myparent</artifactId>
<groupId>mygroup</groupId>
<version>1.0</version>
</parent>
<groupId>mygroup</groupId>
<artifactId>myplugin</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
...
"Loader":
<parent>
<artifactId>myparent</artifactId>
<groupId>mygroup</groupId>
<version>1.0</version>
</parent>
<groupId>mygroup</groupId>
<artifactId>myloader</artifactId>
<packaging>war</packaging>
<version>1.0</version>
...
Now, I need to generate the jar "plugin" and copy it to a defined directory in the project "loader" when I build it.
This have to work locally in eclipse and in jenkins automatically builds.
Someone has any idea how I can do this?
Basically it solved in four steps:
I configure the plugin "maven-assembly-plugin", in my "plugin" project, to copy the jar in a relative path within the project "loader".
In this configuration I specify the property "descriptorRef" in jar-with-dependencies, this helps me to pack the necessary libraries into the jar
I configure the plugin "maven-copy-plugin", in my "plugin" project, to copy the properties in the same relative path within the project "loader"
The pom.xml from my "plugin" project stay like this:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<outputDirectory>${my.output.directory}</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>com.goldin.plugins</groupId>
<artifactId>maven-copy-plugin</artifactId>
<version>0.2.3.6</version>
<executions>
<execution>
<id>create-archive</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<resources>
<!-- Copy config properties -->
<resource>
<targetPath>${my.output.directory}</targetPath>
<directory>${project.basedir}/conf</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
On the other hand, I set the project to consume the plugins from the directory ${my.output.directory}
To automate the build in eclipse I create a new type of program builder for the project. This builder run the command "mvn assembly:assembly".
Step 1:
Step 2:
Step 3: In the tab "Build Options" you can configure details of this execution.
For jenkins, I create a dummy plugin which is used only from the tests.
Run maven install for plugin, which puts the resulting jar in your local maven repo. Then specify plugin in your dependencies section as a dependency. In this case, even though these projects are siblings, they are only siblings in the sense that the parent is specify the build order when mvn goals are run in parent. Their actual build artifacts must be included by the sibling if you intend to use it there.