Maven scope test needs to resolve depdencies - maven

I have a dependency in my pom with <scope>test</scope>. As I understood the scope concept of maven the dependency should only be required during test builds. Nevertheless maven trys to download the dependency during a mvn package which is why I get following build failure:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.278 s
[INFO] Finished at: 2016-04-19T22:11:59+02:00
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project my-module: Could not resolve dependencies for project com.mycompany.app:my-module:jar:1: Failure to find group-a:artifact-b:jar:tests:1.0 in https://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]0
I use following pom:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Is this intended behavior of maven?
Are there any mitigations to ignore the dependency during package/install/deploy builds?
Any help is appreciated

I couldn't solve the issue, but I found a comfortable way to mitigate the problem. Durring my package I use -Dmaven.test.skip=true parameter. So I defined two profiles one with the dependency and one without. When tests are skipped I deactivate the profile with the dependency.
Nevertheless I would appreciate a solution with the scope tag
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>skip-tests</id>
<activation>
<property>
<name>maven.test.skip</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
</project>

Related

Are maven's profile-specific dependencies propagated to the published artifact?

I need to publish an artifact twice - each with a different set of dependencies. I'm happy to give the artifacts different ids but would like to use the same pom.xml file. I've attempted this with profile-specific dependencies, but I can't seem to get these dependencies visible as transitive dependencies in the artifacts. Is that possible?
pom1.xml - creates profile-specific builds. The A profile adds a dependency on commons-collections 3.2.2 and profile B depends on 3.2.1.
pom2.xml - simply defines a dependency on pom1's artifact.
src/main/java/tt.java - defines an empty class to provide something to compile by pom1.
mvn version is 3.8.5.
% mvn -f pom1.xml -P A dependency:tree
...
[INFO] org.acme:testing:jar:testing
[INFO] \- commons-collections:commons-collections:jar:3.2.2:compile
...
% mvn -f pom1.xml -P B dependency:tree
...
[INFO] org.acme:testing:jar:testing
[INFO] \- commons-collections:commons-collections:jar:3.2.1:compile
...
% mvn -f pom1.xml -P A clean install
...
% mvn -f pom2.xml dependency:tree
...
[INFO] org.acme:testing2:jar:testing
[INFO] \- org.acme:testing:jar:testing:compile
[INFO] \- commons-collections:commons-collections:jar:3.2.1:compile
It seems to me pom2's dependencies should have been on 3.2.2 from profile A.
Any idea on how I might make the dependencies from profile A transitive to dependent poms?
pom1.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>org.acme</groupId>
<artifactId>testing</artifactId>
<version>testing</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>A</id>
<properties>
<profile.name>cuda11.2</profile.name>
</properties>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>B</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.name>cpu</profile.name>
</properties>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</profile>
</profiles>
<!-- If I move this dependency out of the profile, then it becomes visible
as a transitive depenency to other projects referencing this pom's artifact.
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
-->
</project>
pom2.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>org.acme</groupId>
<artifactId>testing2</artifactId>
<version>testing</version>
<dependencies>
<dependency>
<groupId>org.acme</groupId>
<artifactId>testing</artifactId>
<version>testing</version>
</dependency>
</dependencies>
</project>

Why "mvn deploy" command is rebuilding .jar?

I am working on the spring-boot java application. I am trying to build and publish .jar using maven with profile.but somehow mvn deploy command rebuilds .jar again.
option 1:I used mvn clear install -Pdev and did mvn deploy -Dmaven.install.skip=truewithout profile and its deploying default .jar file
option 2: I passed profile id during publish too.mvn deploy -Dmaven.install.skip=true ITs working fine but its rebuilding everything again and we do not want to use maven profile name again during mvn deploy
pom.xml
<project...>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profile.id>dev</spring.profile.id>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profile.id>prod</spring.profile.id>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
mvn clean install -Pdev
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) # xyz-
profile
[INFO] Building jar: /sys_apps_01/jenkins/workspace/xyz-profile-0.0.3-
SNAPSHOT.jar
mvn deploy mvn deploy -Pdev
I am getting below logs for both deploy command:
[DEBUG] isUp2date: false (Destination /sys_apps_01/jenkins/workspace/xyz-
profile-0.0.3-SNAPSHOT.jar not found.)
[INFO] Building jar: /sys_apps_01/jenkins/workspace/xyz-profile-0.0.3-
SNAPSHOT.jar
Can anyone help me to understand,why its rebuilding again while deploy ?
You should read about Maven lifecycle for understand what is happening : https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
So when you're doing a deploy command maven trigger all previous goal in the lifecycle :
validate
compile
test
package
verify
install
Then deploy

How should I configure pom.xml to keep A and B in my private local Maven repo cache while using Maven Central and java.net's Maven repositories?

How should I configure pom.xml to keep A and B in my private local Maven repo cache while using Maven Central and java.net's Maven repositories?
I've got two local private projects, call them A and B, but both depend on a few bug fixes in org.javolution:javolution-core-java:6.1.0-SNAPSHOT, which isn't in Maven Central. Both A and B depend on other artifacts which are in Maven Central. Project B depends on project A.
I'm used to using Maven with the local repository (cache) in ~/.m2 and Central, but it seems like I'm not configuring the project correctly for the java.net repository.
<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.redacted</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>B</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<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>com.opera.link.api</groupId>
<artifactId>opera-link-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.javolution</groupId>
<artifactId>javolution-core-java</artifactId>
<version>6.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Java.net Maven Repository</id>
<url>https://maven.java.net/content/repositories/snapshots/</url>
</repository>
</repositories>
</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.redacted</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>A</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<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>org.javolution</groupId>
<artifactId>javolution-core-java</artifactId>
<version>6.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Java.net Maven Repository</id>
<url>https://maven.java.net/content/repositories/snapshots/</url>
</repository>
</repositories>
</project>
Removing the <repositories> element from B produces the obvious result:
------------------------------------------------------------------------
Building B 1.0-SNAPSHOT
------------------------------------------------------------------------
The POM for org.javolution:javolution-core-java:jar:6.1.0-SNAPSHOT is missing, no dependency information available
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 0.547s
Finished at: Wed Jan 29 17:19:48 EST 2014
Final Memory: 3M/15M
------------------------------------------------------------------------
Adding the <repositories> element back in to B gives this on running:
------------------------------------------------------------------------
Building B 1.0-SNAPSHOT
------------------------------------------------------------------------
--- exec-maven-plugin:1.2.1:exec (default-cli) # B ---
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - package com.redacted.A.posts does not exist
at com.redacted.B.App.<clinit>(App.java:30)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 1.219s
Finished at: Wed Jan 29 17:50:45 EST 2014
Final Memory: 4M/15M
------------------------------------------------------------------------
As I'm typing this, I'm realizing perhaps this might not be a Maven problem as much as a NetBeans problem. I clicked the "Re-Run Goals" button expecting a clean-and-build (as that's what I last executed), but doing a real clean-and-build seems to compile correctly, while "Re-Run Goals" just makes mvn run the exec goal. Is my pom.xml configured correctly?
Edit: I'm using NetBeans 7.4.
Your first error message means that the javolution-SNAPSHOT couldn't be found. You have to provide a jar with pom in your local repository or buy using the java.net-repository.
The second error message results from running project B by the Exec-Maven-Plugin. The strange message seems a little bit missleading.
I would recommend you:
First put your custom javolution-SNAPSHOT.jar into your local maven repository by using the maven install-goal
If in doubt, which libraries you need from java.net, put the repository location into Project A as well as Project B.
Next install Project A into your local maven repository by using the maven install-goal.
Next install Project B into your local maven repository.

maven compilation fails because unavailable dependency

I'm trying to run java program with maven but when i compiled using the command mvn -U compile
he shows me the following error
[root#onePK-EFT1 tutorials]# mvn -U compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building onePK Java Tutorials 0.6.0.5
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/com/cisco/onep/libonep-core-rel
/0.6.0.5/libonep-core-rel-0.6.0.5.pom
[WARNING] The POM for com.cisco.onep:libonep-core-rel:jar:0.6.0.5 is missing, no
dependency information available
Downloading: http://repo.maven.apache.org/maven2/com/cisco/onep/libonep-
core-rel/0.6.0.5/libonep-core-rel-0.6.0.5.jar
[INFO] ------------------------------------------------------------------------
INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.517s
[INFO] Finished at: Tue Jul 09 07:28:28 PDT 2013
[INFO] Final Memory: 3M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project java-tutorials: Could not resolve
dependencies for project com.cisco.onep:java-tutorials:jar:0.6.0.5: Could not find
artifact com.cisco.onep:libonep-core-rel:jar:0.6.0.5 in central
(http://repo.maven.apache.org/maven2) -> [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
and this 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>com.cisco.onep</groupId>
<artifactId>hello-network-app</artifactId>
<version>0.1-SNAPSHOT</version>
<name>The onePK Hello Network Example Application</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<forkMode>always</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.cisco.onep</groupId>
<artifactId>libonep-core-rel</artifactId>
<version>0.6.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<developers>
<developer>
<name>onePK Team</name>
<email>onepk-feedback#cisco.com</email>
<organization>Cisco.com</organization>
</developer>
</developers>
I think it's because he can't find libonep-core-rel.jar which i have it included
please any help
The simple solution is to use the appropriate maven repository for the artifacts com.cisco.onep* which are not located in Maven central.
As an immediate solution, but not a recommendation, you can use system dependencies to resolve artifacts on your local filesystem.
As #khmarbaise implied, try to publish those corporate artifacts to your corporate Maven repository (Nexus, Artifactory, Archiva, etc.), even an FTP/HTTP server would do...
Once you publish those corporate artifacts to your "corporate" repository(hopefully it's already in place), you just need a new repository declaration in your Maven POM.

Maven 3 Multi Module build tries to run targets on the Multi Module POM itself

I have several projects that all have a similar Maven build. Each of the project POMs extend from a parent POM containing all of the common dependencies, plugins, etc. that are available to each project. I also have a multi-module POM, currently separate from the parent POM. The purpose of this multi-module POM is to have a single place I can run a target on all of the modules with one command.
If I try to run a plugin, say JS Duck, off of the multi-module POM, I get the following output (project names changed for simplicity):
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Project 1 .......................................... SUCCESS [7.850s]
[INFO] Project 2 .......................................... SUCCESS [0.803s]
[INFO] Project 3 .......................................... SUCCESS [8.488s]
[INFO] Multi-Module POM ................................... FAILURE [0.477s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.780s
[INFO] Finished at: Fri Dec 14 09:31:52 EST 2012
[INFO] Final Memory: 17M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'jsduck' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories
It fails because I don't have the JS Duck plugin specified in my multi-module POM.
Why is it trying to run the plugin on the multi-module POM at all?
I can go the other route and include the modules in my parent POM instead of having a separate multi-module POM, but that has a different problem. In that case, the build succeeds because this time the JS Duck plugin is declared in the multi-module/parent POM. However, it runs the JS Duck plugin against the multi-module/parent POM which essentially generates a garbage 'target' directory because there is no code there to analyze.
For reference, my project structure is:
dev
|- Project1
|- pom.xml
|- Project2
|- pom.xml
|- Project3
|- pom.xml
|- pom.xml (parent POM)
|- all-pom.xml (multi-module POM)
Is there any recommended suggestions in this situation? Is there any way to stop Maven from trying to build the multi-module POM itself? Maybe a different <packaging> type that would do that?
Thanks for your suggestions!
[EDIT]
Here is the all-pom.xml... some details changed for privacy.
<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>com.nate</groupId>
<artifactId>projects-all</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>Multi-Module POM</name>
<description>Builds all projects</description>
<modules>
<module>Project1</module>
<module>Project2</module>
<module>Project3</module>
</modules>
<repositories>
...
</repositories>
<pluginRepositories>
...
</pluginRepositories>
</project>
And here is the parent POM... including the addition of child modules.
<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>com.nate.pom</groupId>
<artifactId>js-master</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>JavaScript Project POM</name>
<description>Parent POM for all JavaScript projects.</description>
<!-- inherit license and other company-wide configuration -->
<parent>
<groupId>com.nate</groupId>
<artifactId>master</artifactId>
<version>1.1</version>
</parent>
<modules>
<module>Project1</module>
<module>Project2</module>
<module>Project3</module>
</modules>
<properties>
<release.version>1.0.0</release.version>
</properties>
<scm>
...
</scm>
<issueManagement>
...
</issueManagement>
<ciManagement>
...
</ciManagement>
<developers>
...
</developers>
<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src/main/js</sourceDirectory>
</build>
<profiles>
<profile>
<id>javascript</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
...
</properties>
<dependencies>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>nl.secondfloor.mojo.jsduck</groupId>
<artifactId>jsduck-maven-plugin</artifactId>
<version>0.1.1-SNAPSHOT</version>
<configuration>
<javascriptDirectory>src/main/js</javascriptDirectory>
<targetDirectory>target/site/api</targetDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<repositories>
...
</repositories>
<pluginRepositories>
...
</pluginRepositories>
</project>
You can explicitly deactivate the plugin in the all-pom
<plugin>
<groupId>nl.secondfloor.mojo.jsduck</groupId>
<artifactId>jsduck-maven-plugin</artifactId>
<version>0.1.1-SNAPSHOT</version>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
The pom which contains the modules list:
<modules>
<module>..</module>
..
</modules>
must have the packaging type pom.

Resources