maven - advice on usage of multi-modules (jar, war, …) project - java

I have the following Maven organization:
- ./pom.xml (top-level project, which defines the 4 modules below)
- ./a/pom.xml (library jar)
- ./b/pom.xml (library jar)
- ./c/pom.xml (war)
- ./d/pom.xml (executable jar)
For building and deploying the c project, I do:
mvn verify tomcat7:redeploy -pl c -am
For building and executing the d project, I do:
mvn verify exec:java -pl d -am
For d there is a problem. Maven reports:
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli)
on project parent: The parameters 'mainClass' for goal
org.codehaus.mojo:exec-maven-plugin:1.4.0:java are missing or invalid -> [Help 1]
This is indeed correct, the parent pom.xml has no configuration for exec. Only the d project has a configuration for exec.
This question is a little variation of question maven - advice on usage of multi-modules (jar, war, ...) project, so I decided to dedicate a separate question for it.
Why does Maven try to exec on the parent project?
update
I abstracted the same problem in following three pom files:
The parent 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>a</module>
<module>b</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The a 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>a</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>a</name>
</project>
The b 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>b</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>b</name>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>a</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>MyMainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
The command I am executing is:
mvn verify exec:java -pl b -am
This can also be found at https://github.com/jeperjaperjieper/issue-33197130.

I can't cure your problem, but I can give you a recipe. You may have hit a maven issue.
You don't have to use -pl and build from the top every time.
I would expect it to work if you just run 'mvn' (no -pl) at the top; it probably doesn't take very long to build the first pieces.
If you don't want to do that, then you need to run 'mvn install' for the pieces that come first, and then you'll be able to cd into the last piece and build it.
I recommend opening up a JIRA issue for Maven on this; it seems as if you've exposed a problem with -pl, but the best way to find out is to post an issue with a test case and poke the Maven user mailing list.

Try mvn exec:java -Dexec.mainClass="com.example.Main" to run your d project.

Related

maven error using intellij is referencing itself

I have this 2 simple pom.xml
main parent project and module which the project is using
main project 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.clouddispatcher</groupId>
<artifactId>clouddispatcher</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>aws-manager</module>
</modules>
<dependencies>
<dependency>
<groupId>com.clouddispatcher</groupId>
<artifactId>aws-manager</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<aws.java.sdk.version>1.11.875</aws.java.sdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>${aws.java.sdk.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
and the module 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">
<parent>
<artifactId>clouddispatcher</artifactId>
<groupId>com.clouddispatcher</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-manager</artifactId>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
</dependency>
</dependencies>
</project>
now when I run :
mvn clean install
I keep getting this error :
The build could not read 1 project -> [Help 1]
The project com.clouddispatcher:aws-manager:1.0-SNAPSHOT (C:\Dev\my\java\clouddispatcher\aws-manager\pom.xml) has 1 error
'dependencies.dependency. com.clouddispatcher:aws-manager:1.0-SNAPSHOT' for com.clouddispatcher:aws-manager:1.0-SNAPSHOT is referencing itself. # com.clouddispatcher:clouddispatcher:1.0-SNAPSHOT, C:\Dev\my\java\clouddispatcher\pom.xml, line 23, column 21
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/ProjectBuildingException
Let me explain the points of khmarbaise in more detail:
Parents cannot have Java classes. You need to remove the classes from the parent.
You cannot define circular dependencies, i.e. if A is a parent of B, it cannot have B as dependency.
You need to refactor your project to meet these criteria, e.g. by moving the classes from the parent to a second module.

How to override profile property from command line [duplicate]

I have the following plain pom running by Maven 3.0.4.
<?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.example</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
I am trying to override default settings in command line like this:
mvn -Dproject.build.finalName=build clean package
But this this is ignored, and I get test-1.0.jar. I've tried to change another properties, like outputDirectory, directory, artifactId, but also failed.
What is the proper way to do this thing?
See Introduction to the POM
finalName is created as:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
One of the solutions is to add own property:
<properties>
<finalName>${project.artifactId}-${project.version}</finalName>
</properties>
<build>
<finalName>${finalName}</finalName>
</build>
And now try:
mvn -DfinalName=build clean package

Artifactory/JFrog is producing the wrong parent artifactId in generated POM

I have encountered a strange bug while trying to deploy new release builds to my company's JFrog.io (fka: ArtifactoryOnline.com) repository. The project I'm working on has multiple maven modules, all under a single parent. When I upload the JAR files that maven builds to JFrog, the generated POM file is being populated with the incorrect parent artifactId.
I spent a great deal of time and could not find any problems with my company's codebase for the project, so I decided if I could reproduce the error from scratch. Thus I made this throwaway project to demonstrate the bug. For those uninterested in downloading the full project, here are the relevant POM files:
The parent POM, "blueberry":
<?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>org.nbyrd</groupId>
<artifactId>blueberry</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>blueberryjam</module>
<module>berries</module>
</modules>
</project>
Dependant POM 1: "berries":
<?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">
<parent>
<artifactId>blueberry</artifactId>
<groupId>org.nbyrd</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>berries</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Dependant POM 2, "berry-jam":
<?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">
<parent>
<artifactId>blueberry</artifactId>
<groupId>org.nbyrd</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blueberry-jam</artifactId>
<dependencies>
<dependency>
<groupId>org.nbyrd</groupId>
<artifactId>berries</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
(For what it's worth, all of these POM files were generated in their entirety by JetBrains IntelliJ.)
Pretty straight forward at this point: I have two Maven modules--"berries" and "berry-jam"--that are housed under a central parent module, "blueberry". berry-jam uses berries as a dependency, although I'm not sure if that's relevant to the problem.
When I build the project, I get two JAR files: berries-1.0.jar and berry-jam-1.0.jar. This is as expected. At this point, I would upload these artifacts to Artifactory so that other projects may reference them. And this is where the trouble starts.
When I upload berries-1.0.jar, Artifactory generates the following 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">
<parent>
<artifactId>berries</artifactId>
<groupId>org.nbyrd</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>berries</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
As you can see, the generated POM defines the parent artifactId as "berries", which is wrong.
The same problem occurs when I upload berry-jam-1.0.jar:
<?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">
<parent>
<artifactId>blueberry-jam</artifactId>
<groupId>org.nbyrd</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blueberry-jam</artifactId>
<dependencies>
<dependency>
<groupId>org.nbyrd</groupId>
<artifactId>berries</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
In both cases, I would expect the parentId to be "blueberry", as it is explicitly stated in both of the child-modules' POM files. These incorrect artifactIds make it impossible to use these libraries. If a third-party application attempts to use these libraries, maven will produce the following warning when downloading dependencies and the build will fail:
[WARNING] The POM for org.nbyrd:berries:jar:1.0 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
This functionality used to work correctly in the past when uploading to Artifactory, so I'm not sure if something has changed in how the tool works or if I have simply done something wrong. Any thoughts?
When you use mvn deploy maven deploys the pom.xmlfile as separated artifact. But when you uploads the jar file with artifactory's web ui and asks artifactory to generate the pom file, I guess it generates it from the data presents in the jar. So the artifactory isn't aware about existing of a parent project.
I had similar problem and my guess was that artifactory is trying to fix pom by changing first artifactId value it can find. Not seeing that it's inside parent node.
So I moved parent node below artifactId/version and that worked for me.
<?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>
<artifactId>berries</artifactId>
<version>2.0</version>
<parent>
<artifactId>berries</artifactId>
<groupId>org.nbyrd</groupId>
<version>1.0</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Maven cannot resolve dependency for module in same multi-module project

When running commands such as
mvn dependency:build-classpath
or
mvn exec:java
Maven is unable to resolve a dependency of one of my modules on another.
[ERROR] Failed to execute goal on project parser-app: Could not resolve dependencies for project project_group:A:jar:0.1-SNAPSHOT: Could not find artifact project_group:B:jar:0.1-SNAPSHOT
The project structure is as follows:
/pom.xml
/A/pom.xml
/B/pom.xml
The parent pom is as follows:
<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>project_group</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>0.1-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>A</module>
<module>B</module>
</modules>
The first child module (the one failing to resolve the dependency):
<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>
<parent>
<groupId>parent_group</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>A</artifactId>
<packaging>jar</packaging>
<name>A</name>
<dependencies>
<dependency>
<groupId>parent_group</groupId>
<artifactId>B</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
The second child module (the dependency):
<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>
<parent>
<groupId>parent_group</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>B</artifactId>
<packaging>jar</packaging>
<name>B</name>
Have you run mvn clean install at least once on the project to install the dependencies within your local repository?
The Maven reactor is weird that way, it keeps modules around only for certain tasks. When running a build target that only does something for one subproject, then even if Maven builds dependencies first, it does not keep them around in the reactor (sometimes).
Installing to the local repository is a workaround, but it is horrible and should be avoided when possible, because you can easily end up with outdated build results.
A slightly less ugly workaround is to combine two build targets, where the second build target does something harmless, but triggers addition to reactor in all subprojects.
As an example you can combine the task you want with the 'compile' or 'package' tasks.
Also see highest voted answer at
Maven doesn't recognize sibling modules when running mvn dependency:tree
This error might also be caused by Maven being in offline mode.
Sometimes I seem to accidentally enable offline mode in IntelliJ IDEA. To disable it, toggle the Toggle Offline Mode toggle in the Maven Toolbar
or uncheck the Work Offline checkbox in the settings under Build, Execution, Deployment > Build Tools > Maven.
Configuring test-jar in the jar plugin resolved the issue for me:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Afterwards, running mvn clean install works.

compile and test-compile fail

I have two maven projects, one named common and another core. core depends on common. The dependency span both src and test code in core. Here's the file structure:
common/
src/main/java/
...
src/test/java/
...
pom.xml
core/
src/main/java/
...
src/test/java/
...
pom.xml
Common's pom is setup pretty simply:
<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.uttt.common</groupId>
<artifactId>common</artifactId>
<version>0.1.0</version>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
...
</plugin>
</plugins>
</build>
</project>
My core's 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.uttt.core</groupId>
<artifactId>core</artifactId>
<version>0.1.0</version>
<dependencies>
...
<dependency>
<groupId>com.uttt.common</groupId>
<artifactId>common</artifactId>
<version>0.1.0</version>
<scope>?</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
...
</plugin>
</plugins>
</build>
</project>
The problem is, when the scope on my dependency on common in core/pom.xml is compile or provided, the compile maven goal passes but test-compile doesn't - with compilation errors in the test code. When the scope is test, the neither goals passes - with compilation errors in the source code.
Anyone have any clues on how to setup my poms such that compile and test-compile succeed?

Categories