In maven multi-module project, how to package a single child-project? - java

I'm working on a multimodule maven project. Some time ago, the project was partitioned into subprojects:
"Common" JAR project
"User" WAR project, depending on Common
etc.
It's the same project as described here, in this question.
When I invoke mvn package at superproject level, command succeeds and resulting war can be deployed. But if I try to invoke mvn package at subproject level, command fails.
[ERROR] Failed to execute goal on project User: Could not resolve
dependencies for project xxx.xxx:User:war:0.0.1-SNAPSHOT: Failed
to collect dependencies for [xxx.xxx:Common:jar:0.0.1-SNAPSHOT (compile), ...
Apparently maven is trying to download my own project (Common) from remote repo...
Downloading: http://repository.jboss.org/maven2/xxx/xxx/Common/0.0.1-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/glassfish/xxx/xxx/Common/0.0.1-SNAPSHOT/maven-metadata.xml
[WARNING] Could not transfer metadata xxx.xxx:Common:0.0.1-SNAPSHOT/maven-metadata.xml from/to jboss (http:/
/repository.jboss.org/maven2): Access denied to: http://repository.jboss.org/maven2/xxx/xxx/common/0.0.1-SNA
PSHOT/maven-metadata.xml
What could possibly be wrong in my config?

When you run mvn package from superproject, it creates the artifacts in the respective target folder, but does not install them in your local repository.
So, when you run mvn package on a subproject, which has a dependency on a sibling module, it looks for the dependency in your local repository, does not find it and thus attempts to download it from the various repositories configured.
Once you successfully run an mvn install from superproject, an mvn package on the subproject should work.

You have a (JBoss) repository entry either in your pom.xml or settings.xml.
To access this repository it is required to be authenticated. If you call the path with your browser, you get also "403 Forbidden". You have to add a public repository of Jboss. (http://repository.jboss.org/)

I have spent a lot of time to find out this sort of 'ERROR', thanks to Raghuram, I was stupid when trying to find the solution but there was actually no error, just because I did mvn install in a submodule which depends on dependencies of other modules, It should work fine when i run mvn install/package on parent scope.

Related

How can I recover maven dependency issues after changing my Intellij license user account to a new account?

I changed the Intellij license user account I have been using and all my maven dependencies are no longer visible to my spring boot project. I deleted the repository folder in C:\Users{My Username}.m2, executed mvn dependency:tree and run mvn clean install. This has recovered my maven dependencies downloaded online. However, the dependencies included as POM from other projects residing on my system are still not visible.
You need to install other projects so that they are in the local repository and visible to the current project.
Even without IntelliJ, you can simply go to each of your other local project folders and do a:
mvn dependency:resolve
Using that resolve mojo, that would be enough to download the dependencies of those projects back to your new %USERPROFILE%\.m2 cache folder.

How to run spring project offline mode?

I am trying to run a Spring Project, with several dependencies, that I have already downloaded into my local repository, and have also done followings :
Added local repository path in setting.xml in localRepository tag.
Running the project using mvn install -nsu -llr -o , for offline mode.
And I am getting following error :
[ERROR] Failed to execute goal on project file2Json: Could not resolve
dependencies for project
com.nse.file2Json:file2Json:jar:0.0.1-SNAPSHOT: Cannot access central
(http://repo1.maven.org/maven2) in offline mode and the artifact
com.fasterxml.jackson.dataformat:jackson-dataformat-csv:jar:2.8.4 has
not been downloaded from it before. -> [Help 1]
I have the dependency installed, take a look :(
I have following questions :
Why is the project is trying to look into http://repo1.maven.org/maven2 when I am using offline mode ?
How can I run the project offline, by providing the required dependencies at required paths ?
Please help !
thanks in advance.
It says it cannot access Maven Central because you are in offline mode. And it tries to look there, because ´jackson-dataformat-csv` is not in your local repository, so Maven needs to get it from somewhere (and Maven Central is the default repository).
What you need to do is to execute mvn dependency:go-offline before you switch to the offline mode. This will download everything that you need (dependencies, transitive dependencies as well as plugins).

Unable to build Shopizer project

When I execute the mvn install inside the project sm-core of Shopizer, I am getting the below error:
[ERROR] Failed to execute goal on project sm-core:
Could not resolve dependencies for project
com.shopizer:sm-core:jar:2.0.3-SNAPSHOT: The following
artifacts could not be resolved: com.shopizer:sm-core-model:
jar:2.0.3-SNAPSHOT, com.shopizer:sm-core-modules:jar:2.0.3-SNAPSHOT:
Failure to find com.shopizer:sm-core-model:jar:2.0.3-SNAPSHOT
in http://maven.springframework.org/snapshot was
cached in the local repository, resolution will not be
reattempted until the update interval of
org.springframework.maven.snapshot has elapsed or updates
are forced -> [Help 1]
How can I solve it?
The Shopizer project is actually a multi-module Maven project. This means that it has a root pom.xml of packaging pom and declares several <modules>.
Those modules, which are Maven project themselves, are typically found as sub-folders under this root folder, which is the case here: there are 4 sub-folders, which are exactly the 4 Maven modules. To compile those sort of projects, you need to invoke Maven from the root project, in this case, shopizer.
You may ask why. In a multi-module Maven project, there is generally inter-dependencies: one module is dependent on another. In this case, the scm-core module that you're trying to build has dependencies on both sm-core-model and sm-core-modules, which are two modules of that project. So when you're building it, Maven will try to look in your local repository those artifacts. The problem is that it won't find them: they have never been built before. Maven won't also find them on a public repository because we're dealing with SNAPSHOT dependencies. The solution is to start the build on the top project: Maven will order correctly the reactor so that when it builds scm-core, both those dependencies will have been built.
Put simply: in a multi-module Maven project, you should practically always invoke Maven commands from the root POM.
To solve your problem, you can check-out the 2.0.3 branch (which is the latest branch) and invoke mvn clean install on the root POM. I did just that successfully.

Download dependencies to local repository but not to "target" one

I have a project, that has pom.xml and depends on lots of dependencies from outside (located far-far in internet..).
So, I want to download all those dependencies which I depend on to my "local repository".
This is my try (I do not need do compilation, so I use "validate" here. So I'm do not expect to have "target" folder in the end):
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:copy-dependencies
In the end - yes I have many dependencies been downloaded to "C:\my\.m2\repository", but some of them went to: C:\projects\myProject\java\trunk\target\dependency, like these ones:
junit-4.8.1.jar
log4j-1.2.8.jar
mockito-all-1.8.2.jar
Question is: how to make those to be downloaded to "C:\my\.m2\repository" but not to "target" of my project?
For now, because of that, another projects that depend on that are failing while building, because they are expecting to find "junit-4.8.1.jar" in local repo.
Another try:
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:resolve
Then those dependencies are not resolvable at all.
Could not resolve dependencies for project bla-bla-SNAPSHOT: The
following artifacts could not be resolved:
commons-lang:commons-lang:jar:2.4, log4j:log4j:jar:1.2.8,
junit:junit:jar:4.8.1: Could not find artifact
commons-lang:commons-lang:jar:2.4 -> [Help 1]
Maven did that because you invoked the goal dependency:copy-dependencies. It will copy the dependencies of the current module to
${project.build.directory}/dependency
See http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
I really can't imagine these dependencies did not get into your local repo. Fiddling around with a script may not be the solution to your problem. Try
mvn process-resources -U -Dmaven.repo.local=C:\my\.m2\repository
The -U option forces a download of all dependencies. I suggest using process-resources, although as of my understanding validate should be fine, too.

Why does maven site:site fail in a multi-module project?

I have a Maven-3 multi-module project:
/root
pom.xml
/bar
pom.xml
/foo
pom.xml
Module foo depends on module bar, and they both have the same parent: root. Everything works fine, until I decided to clean my local repository and run mvn site:site. Build fails with a message:
Failed to resolve artifact.
Missing:
1) com.XXX:bar:jar:1.0-SNAPSHOT
It is a known bug or I'm doing something wrong? I didn't configure maven-project-info-reports-plugin anyhow in any pom.xml yet.
If you do site:site, you are running the specific site goal and not the build cycle, thus the project will not build, and since you just cleaned the repository, the artifact will not be there anywhere to use.
See following links for more information about build lifecycles in maven:
http://www.sonatype.com/books/mvnref-book/reference/lifecycle.html
http://www.sonatype.com/books/mvnref-book/reference/lifecycle-sect-package-specific.html
Perhaps you should try goal site:stage on the root/master-pom.
I didn't try this with src/site in the childs, cause i only have a src/site in the master.
But this work fine for me with Maven 3.0 and a menu ref="reports" in roots site.xml and
putting maven-project-info-reports-plugin in the childs pom.xml

Categories