Cannot generate runnable JAR for very simple Eclipse Maven project - java

I would like to generate a runnable JAR for my Eclipse Java projects. The JAR should contain all my code, with unmodified namespaces, classes, resources etc.
My workspace is organized into a multitude of different projects.
When I right-click a project node in Project Explorer, and select "Run As > Maven install", I get the following:
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< foo.Project1:foo.Project1 >----------------------
[INFO] Building foo.Project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for foo.Project2:foo.Project2:jar:0.0.1-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.220 s
[INFO] Finished at: 2020-06-26T12:20:11+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project foo.Project1: Could not resolve dependencies for project foo.Project1:foo.Project1:jar:0.0.1-SNAPSHOT: Could not find artifact foo.Project2:foo.Project2:jar:0.0.1-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
foo.Project1 runs fine under Eclipse. Its pom.xml file is:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.Project1</groupId>
<artifactId>foo.Project1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>foo.Project2</groupId>
<artifactId>foo.Project2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
(A ZIP of the two projects can be accessed here: https://wetransfer.com/downloads/b9bb4aa840daa6e7b2dd2c793acdb56420200626103047/32c0fd)
I tried the first solution suggested in How can I create an executable JAR with dependencies using Maven? (with 2300+ score) by:
1: adding the following in the build section of the pom.xml:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>foo.Project1.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
2: right-clicking the project node, then "Run As > Maven build..."
3: defining "Goals" as: clean compile assembly:single, and hitting Run
Same errors.
What am I doing wrong?

If you want to use foo.Project2 as dependency, you need to build it first with clean install.
You always need to build a project before you can use it as dependency in the Maven build.
Eclipse cheats a bit by also allowing you to reference projects from the workspace and therefore allows you to run the program. But for the "real build", you need to build everything in the right order.

Related

com.sun.tools.xjc.maven2 giving No XSD files found even though xsd exist (java 11)

I am upgrading from java 8 to java 11. I'm attempting to use jaxb2-maven plugin to convert XSD files into java classes. when i run "mvn clean install" i get the below compile error
[INFO] Ignored given or default sources [xsd/dummy.xsd], since it is not an existent file or directory.
[INFO] Ignored given or default sources [xsd/MessageProcessingAck.xsd], since it is not an existent file or directory.
[WARNING] No XSD files found. Please check your plugin configuration.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.216 s
[INFO] Finished at: 2022-11-10T19:13:17-08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (Acknowledgement notification XML/WSDL binding) on project dsched-integration-ackn: : MojoExecutionException: NoSchemasException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>Acknowledgement notification XML/WSDL binding</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/</schemaDirectory>
<outputDirectory>${basedir}/target/generated/</outputDirectory>
<packageName>com.dgp.ds.ackn.ws.send.acknowledgement</packageName>
<sources>
<source>xsd/MessageProcessingAck.xsd</source>
</sources>
<args>-wsdl -b ${basedir}/src/main/resources/xsd/</args>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I have tried following the directions from these posts. I still got the same error
Maven jaxb2:xjc failing to generate code
the above post is mostly dealing with a lower version that is not compatible with most of my dependencies
https://artofcode.wordpress.com/2019/02/26/jaxb2-maven-plugin-2-4-and-java-11/
This post did not have any effect, both XSD were still not found
I was able to resolve this issue by using the below plugins
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.1</version>
....
</build>

Maven build failure classNotFoundException

It's my first time trying out maven and I can't understand why I keep getting classnotfoundexception every time I am trying to build. This is the error I am receiving:
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< owmapi:owmapi >----------------------------
[INFO] Building OwmAPICase 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) # owmapi ---
[WARNING]
java.lang.ClassNotFoundException: owmapi.Main
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:246)
at java.base/java.lang.Thread.run(Thread.java:832)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.582 s
[INFO] Finished at: 2021-02-28T14:09:17+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project owmapi: An exception occured while executing the Java class. owmapi.Main -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
And this is 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>owmapi</groupId> <!-- case.se.ctk -->
<artifactId>owmapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OwmAPICase</name>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>owmapi.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.aksingh/owm-japis -->
<dependency>
<groupId>net.aksingh</groupId>
<artifactId>owm-japis</artifactId>
<version>2.5.3.0</version>
</dependency>
</dependencies>
</project>
Everything works fine if i simply run the project as a java application in eclipse but for some reason it won't work with maven. I guess the problem lies in mainClass in my pom but I've tried several solutions to no avail.
I think your main class have some dependencies on the jar mentioned in the pom.xml. You're simply creating a target jar which doesn't have those dependencies included. You need to create the uber/fat jar which include all the relevant dependencies. You can use this following plugin maven-assembly-plugin for creating the target jar.
Assumption: Main.java class is under package owmapi.
<build>
<finalName>owmapi</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>owmapi.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

Could not find goal 'devmode' in plugin org.codehaus.mojo

I am a beginner in GWT and Maven. I created a new GWT Application project in Eclipse. Then, I converted that project into Maven Project by right-click on Project name => Configure => Convert to Maven Project and I saw that a pom file was generated for that project. Next, I run as project as Maven build but it was not compiled since a goal was not specified there. Actually, I don't understand what exactly have I to write under that goal section, therefore, I wrote package under that and then again I built maven and it compiled successfully.
After that, I tried to run this maven project in Command Prompt on the SuperDevMode using the 2nd step mentioned on Run the GWT Project under the Setting up a new project section. But while following these steps on Command Prompt, I got an error that devmode could not be found. Here is my Command prompt log:
C:\Users\TEST>cd eclipse-workspace/MyWebApp
C:\Users\TEST\eclipse-workspace\MyWebApp>mvn war:exploded
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenApp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-war-plugin:3.1.0:exploded (default-cli) # abcdef ---
[INFO] Exploding webapp
[INFO] Assembling webapp [abcdef] in [C:\Users\TEST\eclipse-workspace\MyWebApp\target\abcdef-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [63 msecs]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.920 s
[INFO] Finished at: 2017-08-30T11:24:53+05:30
[INFO] Final Memory: 12M/107M
[INFO] ------------------------------------------------------------------------
C:\Users\TEST\eclipse-workspace\MyWebApp>mvn gwt:devmode
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.301 s
[INFO] Finished at: 2017-08-30T11:25:02+05:30
[INFO] Final Memory: 8M/107M
[INFO] ------------------------------------------------------------------------
[ERROR] Could not find goal 'devmode' in plugin org.codehaus.mojo:gwt-maven-plugin:2.8.1 among available goals clean, compile, compile-report, css, debug, eclipse, eclipseTest, generateAsync, help, i18n, mergewebxml, resources, run, run-codeserver, source-jar, test -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoNotFoundException
Here is my 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>MyWebApp</groupId>
<artifactId>abcdef</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<name>MavenApp</name>
<description>It is a maven app</description>
</project>
I searched a lot to resolve this error but found no solution for that issue. Please help me to fix this issue as I am very confused on how to fix it.
Edit: After further study, I used mvn gwt:run as an alternative to the command mvn gwt:devmode, but still I got another error on command prompt as given below:
[ERROR] Failed to execute goal org.codehaus.mojo:gwt-maven-plugin:2.8.1:run (default-cli) on project MyWebApp: The parameters 'runTarget' for goal org.codehaus.mojo:gwt-maven-plugin:2.8.1:run are missing or invalid
Without any more information than gwt:devmode, Maven will try to find an appropriate plugin, based on its default settings. It happens that a gwt-maven-plugin exists with org.codehaus.mojo as groupId, fitting in Maven built-in plugin resolution.
But this is not the plugin you're looking for.
You are probably trying to use this one, so just add this to your pom.xml, in the <plugins> section:
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-8</version>
<extensions>true</extensions>
<configuration>
<moduleName>com.example.app.App</moduleName>
</configuration>
</plugin>
Adapting the moduleName if needed.
Well, I was able to solve this issue by copying some dependencies, plugin, and configuration from the pom.xml file (which is generated in the project created by the webAppCreator using maven) to the pom.xml file of my project created in the Eclipse. So, this is my pom.xml file created finally:
<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>MyPersonalProject</groupId>
<artifactId>MyPersonalProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Setting maven.compiler.source to something different to 1.8
needs that you configure the sourceLevel in gwt-maven-plugin since
GWT compiler 2.8 requires 1.8 (see gwt-maven-plugin block below) -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Don't let your Mac use a crazy non-standard encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- ensure all GWT deps use the same version (unless overridden) -->
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>2.8.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-6</version>
<executions>
<execution>
<goals>
<goal>import-sources</goal>
<goal>compile</goal>
<goal>import-test-sources</goal>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<moduleName>mypackage.MyPersonalproject</moduleName>
<failOnError>true</failOnError>
<!-- GWT compiler 2.8 requires 1.8, hence define sourceLevel here if you use
a different source language for java compilation -->
<sourceLevel>1.8</sourceLevel>
<!-- Compiler configuration -->
<compilerArgs>
<!-- Ask GWT to create the Story of Your Compile (SOYC) (gwt:compile) -->
<arg>-compileReport</arg>
<arg>-XcompilerMetrics</arg>
</compilerArgs>
<!-- DevMode configuration -->
<warDir>${project.build.directory}/${project.build.finalName}</warDir>
<classpathScope>compile+runtime</classpathScope>
<!-- URL(s) that should be opened by DevMode (gwt:devmode). -->
<startupUrls>
<startupUrl>MyPersonalProject.html</startupUrl>
</startupUrls>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I run this application using maven in the Command Prompt, it ran successfully except showing 2 or 3 warnings in the Command Prompt log. Also, I noticed that the SNAPSHOT generated in the version is different for the pom.xml created by Eclipse as compared to the one created by the maven project on command prompt.

Maven enforcer plugin and missing dependencies

Here is a minimal project:
<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.test</groupId>
<artifactId>enforcer</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce-env</id>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<rules>
<requireProperty>
<property>custom</property>
<message>You must set custom property.</message>
</requireProperty>
</rules>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>enforcer-dep</artifactId>
<version>1.0.0</version>
<classifier>${custom}</classifier>
</dependency>
</dependencies>
</project>
When running:
mvn -Dcustom=some-value validate
Validation goes OK.
When running:
mvn enforcer:enforce
or any phase from validate to process-resources
mvn validate
mvn initialize
mvn generate-sources
mvn process-sources
mvn generate-resources
mvn process-resources
I get expected fail with message:
[...]
[INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce-env) # enforcer ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireProperty failed with message:
You must set custom property.
[...]
But when I run any other (later) phase from compile up to deploy e.g.:
mvn install
I get the error about missing dependency but there is no fail caused by enforcer plugin:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building enforcer 1.0.0
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.test:enforcer-dep:jar:${custom}:1.0.0 is missing, no dependency information available
Downloading: http://repo.maven.apache.org/maven2/com/test/enforcer-dep/1.0.0/enforcer-dep-1.0.0-${custom}.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.983s
[INFO] Finished at: Fri Nov 22 09:22:24 CET 2013
[INFO] Final Memory: 7M/152M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project enforcer: Could not resolve dependencies for project com.test:enforcer:jar:1.0
.0: Could not transfer artifact com.test:enforcer-dep:jar:${custom}:1.0.0 from/to central (http://repo.maven.apache.org/
maven2): Illegal character in path at index 84: http://repo.maven.apache.org/maven2/com/test/enforcer-dep/1.0.0/enforcer
-dep-1.0.0-${custom}.jar -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
It looks for me like enforcer plugin is not executed at all, or dependencies are checked first before project can be build when running compile and later phases.
But why dependencies are not checked when running up to process-resources?
Obviously you seemed to have changed the settings of the maven installation which went wrong based on the error message:
[WARNING] Some problems were encountered while building the effective settings
[WARNING] expected START_TAG or END_TAG not TEXT (position: TEXT seen ...ever maven must make a connection to a remote s
erver.\n |-->\n <s... #111:5) # C:\programs\Maven\3\bin\..\conf\settings.xml
This means first clean up the conf/settings.xml file. Best is to use the default file which came via the installation. If you need to make modification do this in the users settings.xml $HOME/.m2/settings.xml or on Windows C:/Users/UserName/.m2/settings.xml
Apart from that i don't know what you like to achieve by using something like this:
[WARNING] The POM for com.test:enforcer-dep:jar:${custom}:1.0.0 is missing, no dependency information available
Downloading: http://repo.maven.apache.org/maven2/com/test/enforcer-dep/1.0.0/enforcer-dep-1.0.0-${custom}.jar

Maven exec:java goal on a multi-module project

I'm trying to run exec-maven-plugin's exec:java goal on a simple two-module project where one module depends on the other. So far I can't find a configuration that works. Here's a boiled-down test case:
+ exec-multi-module-test/
+ pom.xml
+ module1/
+ pom.xml
+ src/
+ main/
+ java/
+ HelloPrinter.java
+ module2/
+ pom.xml
+ src/
+ main/
+ java/
+ MyMain.java
Here's the parent pom:
<?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.mkscrg.sandbox</groupId>
<artifactId>exec-multi-module-test</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
</plugin>
</plugins>
</build>
</project>
module1's pom:
<?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>
<parent>
<artifactId>exec-multi-module-test</artifactId>
<groupId>com.mkscrg.sandbox</groupId>
<version>1.0</version>
</parent>
<artifactId>module1</artifactId>
</project>
module2's pom:
4.0.0
<parent>
<artifactId>exec-multi-module-test</artifactId>
<groupId>com.mkscrg.sandbox</groupId>
<version>1.0</version>
</parent>
<artifactId>module2</artifactId>
<dependencies>
<dependency>
<groupId>com.mkscrg.sandbox</groupId>
<artifactId>module1</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
This project compiles successfully from the top, but running mvn exec:java -Dexec.mainClass=myMain fails:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] exec-multi-module-test
[INFO] module1
[INFO] module2
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building exec-multi-module-test 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) # exec-multi-module-test >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) # exec-multi-module-test <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) # exec-multi-module-test ---
[WARNING]
java.lang.ClassNotFoundException: MyMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:680)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] exec-multi-module-test ............................ FAILURE [0.363s]
[INFO] module1 ........................................... SKIPPED
[INFO] module2 ........................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.566s
[INFO] Finished at: Mon Jun 18 14:11:54 PDT 2012
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project exec-multi-module-test: An exception occured while executing the Java class. MyMain -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
What's the right way to configure this project to allow exec:java to see MyMain?
EDIT: Here's a gist if you'd like to try this yourself: https://gist.github.com/2950830
EDIT: Clarification: I know it's possible to mvn install and then either run exec:java from module2's directory or use the -pl flag from the top. However, I'd like to avoid running mvn install. It shouldn't be necessary to modify my local repository in order to run this goal in a multi-module project. Just as mvn compile "just works" with a multi-module project, so should other goals/phases.
Goals in a multi-module project, when run from the parent, will run against all modules. I don't think that's what you want. You can try:
mvn exec:java -pl module2 -Dexec.mainClass=MyMain
That might work? More info:
Running a specific Maven plugin goal from the command line in a sub-module of a multi-module reactor project
However, I think it's more intuitive to change directory to the sub-module containing the executable before running it.
You should bind the exec-maven-plugin to a maven lifecycle goal, say verify.
Since you want the plugin to be executed only for module2, define the plugin configurations in the parent pom within pluginManagement. Use the same only in module 2.
Then run the following:
mvn verify -Dexec.mainClass=MyMain.
parent pom
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
module 2
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
See this answer for a single-command alternative without mvn install:
https://stackoverflow.com/a/26448447/1235281
By using skip in the parent pom.xml, you can tell Maven to only run exec:java on a specific submodule.
GitHub:
https://github.com/Oduig/mavenfiddle

Categories

Resources