I have a maven project that depends on another local maven project. For example Project A depends on ProjectB
Directory layout:
-TopDir
-DivergedDir
-someSubDir
-ProjectA pom.xml
-TopDir
-subDir2
-ProjectB pom.xml
In my Project A pom.xml I have:
<dependency>
<groupId>projectB</groupId>
<artifactId>projectB-artifact</artifactId>
<version>projectB-version</version>
</dependency>
When compiling project A I have to do the following:
1) go to project B directory and run mvn install && mvn compile
2) go to project A directory and run mvn compile
Is there a way I can run mvn compile in project A directly without have to go to directory of projectB first? As in reference Project B from Project A in a different way?
Related
I have followed the steps of installing local jar (Project A) in another maven project (Project B) but I'm still not able to use classes of the project A in IntelliJ while programming for Project B.
Steps I followed -
mvn install:install-file -Dfile=<full path to jar of Project A> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DgeneratePom=true
And then I added the dependency in Project B's pom.xml
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Running mvn package is successful, and mvn clean install as well.
I'm able to see my jar in External Libraries as well, And also in .m2/repository
But when I use the classes in project A while writing some program fro Project B, I'm getting "Cannot resolve symbol" in Intellij.
Error in Intellij
I have a project structure like:
However, whenever I run mvn clean test -DskipTests from the Project 1 directory, it ends up NOT including the Project 2 module in the Maven reactor even though I've listed it as a dependency in Project 1 as follows:
<dependency>
<groupId>com.main.sub</groupId>
<artifactId>project2-artId</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
What could be happening and why isnt the dependency being recognized?
From https://maven.apache.org/guides/mini/guide-multiple-modules.html; the reactor "Collects all the available modules to build". (modules of the project that is currently being built)
You need to: create a parent project, with packaging pom; add two modules to this project (one for Project1, one for Project2), and submodules for project1.
Then, when you build the parent project, the build order for all modules are decided by the reactor-
I'm a relative Maven novice and am having difficulties using a locally stored jar as a module within my IntelliJ project - a project I have taken from an online tutorial.
I've brought it into my .m2 folder using:
mvn install:install-file "-Dfile=C:/../resources/myshop-automatedtestscore 3.1.17-SNAPSHOT.jar" "-DpomFile=C:/../resources/myshop-automatedtestscore-3.1.17-SNAPSHOT.pom --Dsources=C:/../myshop-automatedtestscore-3.1.17-SNAPSHOT-sources.jar"
.jar is located at:
C:\Users\daveb\.m2\repository\com\myshop\automatedtests\myshop-automatedtestscore\3.1.7-SNAPSHOT\3.1.17-SNAPSHOT.jar
And I added the dependency in the main pom.xml as follows:
<dependency>
<groupId>com.myshop.automatedtests</groupId>
<artifactId>myshop-automatedtestscore</artifactId>
<version>3.1.17-SNAPSHOT</version>
</dependency>
When I go to Project Structure -> Libraries, I can see the Sources dependency jar file is there in grey so should be fine. However the core project seems to be unable to access the class versions of the file. In Target folder they remain .class but in External libraries they are .java
Apologies if this is a novice or obvious solution. I'm trying to resolve.
You have to install jar using below command -
mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=
You have to give artifacts and other parameters so that you will be able to use in pom file using below code -
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>1.1.GA</version>
</dependency>
After that you have to complie your project so that this dependency will be added in your project.
mvn eclipse:eclipse
mvn clean install
You can press here to force refresh the plugins in Intelij.
Here you can check which repository you have and also which maven you have.
Click on preferences
Another option you have is to perform this :
mvn clean install -U
If your dependency is
<groupId>com.me.example</groupId>
<artifactId>my-example</artifactId>
<version>1.1.0</version>
Then your jar file would be my-example-1.1.0.jar and it will be under \.m2\repository\com\me\example\my-example\1.1.0\my-example-1.1.0.jar.
In your case, your jar file is myshop-automatedtestscore-3.1.17-SNAPSHOT.jar and it should be under
C:\Users\daveb.m2\repository\com\myshop\automatedtests\myshop-automatedtestscore\3.1.17-SNAPSHOT\myshop-automatedtestscore-3.1.17-SNAPSHOT.jar.
After this, do a Maven > Reimport from IntelliJ.
My project structure (multi module) is like this
parent
projectA
projectB
... other modules
parent also actually has a parent (Spring Boot).
I have set up a Jenkins jobs to compile & test on every commit, so it runs:
mvn -f pom.xml clean install
And that all works fine. ProjectB depends on ProjectA (which is like a common classes type of project) and is a Spring boot application. So the dependency information is the regular:
<dependency>
<groupId>Group</groupId>
<artifactId>ProjectA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
ProjectB has a separate job in Jenkins to build the deployable jar file and deploy it to server. So the command there is:
mvn -f ProjectB/pom.xml clean install antrun:run
This fails with a message like:
[WARNING] The POM for Group:ProjectB:1.0-SNAPSHOT is missing, no dependency information available
...
[ERROR] Failed to execute goal on project host-api: Could not resolve dependencies for project Group:ProjectB:1.0-SNAPSHOT: The following artifacts could not be resolved: Group:ProjectA:jar:1.0-SNAPSHOT...
Now I can resolve this by doing a mvn install in the ProjectA directory - I've tested this and it does resolve the issue.
But my question is why should I have to? Shouldn't Maven figure out it should be installing the jar in the local repo?
Thanks in advance
TL;DR Tell maven about the structure of your project.
When you run the command
mvn -f pom.xml clean install
Then maven uses the reactor to work out the order of the modules, something like the following is output:
[INFO] Reactor build order:
[INFO] ProjectA
[INFO] ProjectB
So Maven first builds project A, then builds project B.
When you run the command:
mvn -f ProjectB/pom.xml clean install antrun:run
Then you have a problem; maven isn't starting from the parent - it's starting from a child. It's not told about the hierarchy of projects needed to be built first.
If you want to build a single project from a maven multimodule project, along with dependencies you should use:
mvn -f pom.xml -pl ProjectB -am install antrun:run
Where:
-pl ProjectB is the "project list" option, it tells maven to build these specific projects
-am is the "also make" option, it tells maven to build any projects that the projects in pl are dependant on
Specify the dependencies build part when you run the build:
Guide to Working with Multiple Modules
So I suppose the rule is, always build the parent project first and then run goals on subprojects afterwards.
The fix for me was to run clean install on the parent project first and then have a second build configuration in Jenkins that ran -f ProjectB/pom.xml antrun:run
Main Module
-pom.xml
Module1
jar dependency
Module2
jar dependency
I am new to Maven and am wondering what the best practice is for configuring the above setup.
We are upgrading our version of Alfresco (currently all of the above are compile via ANT) and for testing purposes and to make headway on the project, I initially just compiled Module1 and Module2 using their existing ant build scripts and then did a manual install into the Maven repository. I manually added the dependency in my Main Model pom.
Module1 and Module2 need to be compiled and rebuild every time we update Main Module. They are shared with other projects and can change frequently.
What I am working on at this point is the following:
In Main Module pom.xml, I am using maven-antrun-plugin to run the associated ant build scripts for Module1 and Module2. This is working fine and produces the two jar.
Now I would like to have my main module pom.xml file install the two jars into the maven repository.
My dependencies in my pom.xml have an issue when I update the project version since the pom.xml has not been run yet to install the two jars.
<dependency>
<groupId>ca.gc.hc.nhpd</groupId>
<artifactId>EformExtractor</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ca.gc.hc.nhpd</groupId>
<artifactId>SubmissionConsumer</artifactId>
<version>${project.version}</version>
</dependency>
Am I going about this incorrectly? It will work if I run the ant script and install manually and then add the correct version in the pom.xml dependency. We wish this to happen automatically though. We don’t want to have to do this manually for every build we do.
Perhaps you should structure this project with parent and child pom.xml files:
MainModule
|-- pom.xml
|-- ModuleA
| |--pom.xml
|-- ModuleB
|--pom.xml
The parent pom.xml would look something like the one in this example.
The pom.xml for ModuleA and ModuleB could run their Ant scripts, if necessary. Then you should be able to run mvn install at the parent level to install ModuleA and ModuleB to your local Maven repository.