Maven build does not compile - java

I am trying to compile a maven project but I get the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project storm-example: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1]
I am new to maven projects actually. The thing is I am not using the codehous.mojo plugin at all. From what I saw from different existing issues, the people who got this were using the mojo plugin in the build, but in my case i just need the maven-assembly-plugin. Do I need mojo in every maven object despite not explicitly needing it?
The following is my maven pom.xml file
<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.learningstorm</groupId>
<artifactId>storm-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>storm-example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass />
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

The error is a complaint about your following tag in the maven-assembly-plugin's configuration :
<archive>
<manifest>
<mainClass />
</manifest>
</archive>
Unless you want to create an executable jar (in which case you should specify the qualified name of the "main" class), you should just remove the whole <archive> tag.
It looks like you've based your work around the "Creating an Executable JAR"
example of the maven-assembly-plugin's usage page while you might only need what is defined in the first basic example.

Related

Maven dependencies doesn't get exported into the jar

I have a project which I want to export as jar (for some reasons it's impossible to export it as a runnable jar). I have 3 maven dependencies, gson, io and junit, but when I execute the maven built jar in console it says this:
Check my build path:
I export it this way (Eclipse):
Run as -> Maven build...
(mvn) package
And here is my pom:
<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>Carlos</groupId>
<artifactId>Buscaminas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Buscaminas</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<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>res.application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
Also the maven build result:
My project didn't seem to have the correct project structure so I crated a new (maven) project and migrated my packages to the src/main/java folder and then used this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>res.application.Main</mainClass> <!-- Or wherever is your main method-->
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
And executed mvn package
That created a "jar-with-dependencies" jar also with all the recources (images, interface fxml files...).
From what I understand what you need might be this one
mvn install dependency:copy-dependencies
The built jar does not contain the dependencies, you must either provide them in classpath when executing the jar or have build process copy them into your jar, creating a so-called uber-jar. Good way to achieve the latter is using maven-shade-plugin.

Java Maven Clean and Build With Dependencies

im still new in java maven and dependencies. can i ask?
I create some project about QR code Generator with using Java Maven.
When I run my project into Netbeans, using qrgen-1.2.jar, core-2.0.jar and javase-2.0.jar. IT CAN GENERATE ANY QR CODE THAT I WANT.
But when i try to Build and Clean, it CANNOT generate my QR code in my Document/NetbeansProjects/QRcode/target/QRcode-1.0-SNAPSHOT.JR
Here 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>QRcode</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>com.mycompany.qrcode.QRcode</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<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>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
Based on your POM, you aren't packaging your dependencies in the executable jar being generated. This is causing your program to fail when you run it outside of the IDE.
Here's an example of how to use the Maven Assembly plugin to create an executable jar that includes your dependencies:
<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>com.mycompany.qrcode.QRcode</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
The output of mvn package will now include target/QRcode-1.0-SNAPSHOT-jar-with-dependencies.jar, which you can see includes the classes specified by your build dependencies.
Here's a link to the documentation for the plugin.

Microsoft Event hub send java

I am following this tutorial on Microsoft website to get a simple send event to Azure event hub to work in java. However, I used maven and followed every thing but the final jar file runs with exception errors. Please here is what I did.
I created simple Maven project in Eclipse Juno and here is the content of my pom.xml
`<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">
<description>Java libraries for talking to Windows Azure Event Hubs</description>
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>send</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<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>com.bd.Send</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-eventhubs</artifactId>
<version>0.14.0</version>
</dependency>
</dependencies>
</project>`
Is this pom.xml file correct? as when I run the final jar file, i get exception errors but the project compiles correctly.
How can I configure the send event hub java project using maven ??
Thanks in advance
This is the pom.xml file that worked:
<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>send</groupId>
<artifactId>send</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>osssonatype</id>
<name>OSS Sonatype</name>
<url>https://repo1.maven.org/maven2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-eventhubs</artifactId>
<version>0.14.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>sdasf.Send</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
When Run as Maven install. Inside the target folder there will be 2 jar files. Running the one with dependencies solved my issue.
on the cmd command line:
java -jar send-0.0.1-SNAPSHOT-jar-with-dependencies.jar

How to create a executable jar file using Maven and Netbeans

I'm trying to create a runnable jar file, but somehow my Maven/Netbeans fails to do so. This 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>de.ksw</groupId>
<artifactId>KBSE-CDI-Testprogram</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<version>1.8.1</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>de.ksw.kbse.cdi.testprogram.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>KBSE-CDI</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>1.8.1</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>
</project>
As you can see I already tried to add the main class. I also added the dependency maven-jar-plugins, but I still get an error messagen when I try to compile the project:
Plugin org.apache.maven.plugins:maven-jar-plugin:1.8.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-jar-plugin:jar:1.8.1: Failure to find org.apache.maven.plugins:maven-jar-plugin:pom:1.8.1 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
What am I doing wrong here? How to fix this?
What I do in one project of mine is to use the maven-jar-plugin and the maven-assembly-plugin
The result is an executable fat-jar.
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>de.ksw.kbse.cdi.testprogram.App</mainClass>
</manifest>
<manifestEntries>
<!--this one is to add the version information in the manifest. This is something that I need but you can ignore it-->
<version>${project.version}</version>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<classifier>DEV</classifier>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- to inheritance merges -->
<phase>package</phase> <!-- just to use in the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!--This plugin is used to have just one jar instead of 2-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<finalName>${project.artifactId}</finalName>
<outputDirectory>${project.build.directory}</outputDirectory>
<verify>false</verify>
</configuration>
</plugin>
After this just it the clean and build button in netbeans and search for something
with assembly file: PATH_TO_JAR/KBSE-CDI-Testprogram.jar
There is no version 1.8.1 of maven-jar-plugin.
Here the list of all available versions of maven-jar-plugin : https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin
You have to update version into an earlier version.
Example with the latest version :
<version>3.0.2</version>

maven how to include dependencies correctly

I was trying to figuring out how to use maven but I didn't get it.
First I tried sqlite 3.8.7 from :
mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7
I can compile well but when I try to execute maven doesn't find sqlite jar file, so I try to use :
mvn install:install-file
but It didn't work too, so I just used -cp and I have fixed.
Second I try to use jfreechart from :
http://mvnrepository.com/artifact/jfree/jfreechart/1.0.13
I did same steps below for jfreechart but this time it gives me NoClassDefFoundError.
Both of them works when I compile manually but with maven it's not. What I am missing about it ? If I always add manually like sqlite why should use maven anyway ?
Notes : I compile as :
mvn compile
and I'm packaging as :
mvn package
and finally I try to execute as :
java -cp target/porject.jar org.path.App
Edit :
This is for jfreechart app(pom.xml)
I add dependency tags from mvnrepository.com
<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>org.project</groupId>
<artifactId>ChartTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ChartTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
</project>
Thanks in Advance
All that you need to create your jar executable with your dependencies and launch it as a standalone app:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.mypackage.main.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
and then run goal :
clean package assembly:single
Your problem is you don't include your dependencies in your target jar.
Add this to your pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>attached</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then you can start your app using the command line:
java -cp target/ChartTest-1.0-SNAPSHOT-jar-with-dependencies.jar org.path.App
Don't use hyphen in commands, use mvn package and mvn compile

Categories