Resolve dependencies with maven in java - java

I am trying to make a mojo that takes all the unit tests from all the modules that have a certain annotation made by me. The problem is that I can't access the unit tests from any module.
The module structure looks like this:
|--ModuleA (depends on Module D)
|--ModuleB (depends on Module D)
|--ModuleC (depends on Module D)
|--ModuleD (the mojo)
The question is how to access or retrieve the unit test classes of each module when the mojo runs for it.

Updated Response since you told us about the dependencies of the modules.
Best way to do this is :
- moduleA, moduleB, moduleC built with maven
- generate test jars : add in your pom
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>test-jar</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
moduleD has dependcy on moduleA.jar,moduleB.jar,moduleC.jar & moduleA-test.jar,moduleB-test.jar,moduleC-test.jar
add in your moduleE's pom.xml for each module :
<dependency>
<groupId>com.rizze</groupId>
<artifactId>moduleA</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.rizze</groupId>
<artifactId>moduleB</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.rizze</groupId>
<artifactId>moduleC</artifactId>
<version>1.0.0</version>
</dependency>
Module E is here to call all the test of Module A,B,C,D in the order you want to. Module E is making integration of all the modules in order to perform all the tests (as requested by you).

Related

Maven Unresolved Reference when using version from property

I have a maven project project A that has a dependency on a different maven project project B. I publish Project B as two jars, one with the regular class files and one for its tests (I need to re-use some componenents). Maven has a plug-in for this.
The pom.xml for project A looks like this:
<properties>
<project-B.version>abcd123</project-B.version>
</properties>
<dependencies>
<dependency>
<groupId>group-id-project-B</groupId>
<artifactId>project-B</artifactId>
<version>${project-B.version}</version>
</dependency>
<dependency>
<groupId>group-id-project-B</groupId>
<artifactId>project-B</artifactId>
<classifier>tests</classifier>
<type>test-jar</type>
<version>${project-B.version}</version> <!-- Doesn't work! -->
<scope>test</scope>
</dependency>
</dependencies>
Somehow, both my IDE (IntelliJ) and mvn CLI are unable to compile this and both give an unresolved reference in my test classes (where I re-use test files from project B).
If, however, I replace ${project-B.version} with abcd123 in the test-jar dependency everything loads fine. I can't make any sense of this as to why mvn doesn't like me to use a variable in properties? This behaviour is consistent between my IDE and mvn cli.
For what it's worth, this is the output for project B when I use mvn dependency:list
group-id-project-B:project-B:test-jar:tests:abcd123:test
group-id-project-B:project-B:jar:tests:abcd123:compile
And the rest of my pom:
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
is there a bug in maven or am I missing something?
Edit:
I use mvn clean install and the truncated output is:
[INFO] --- kotlin-maven-plugin:1.3.72:test-compile (test-compile) # optimization-engine-service ---
[ERROR] MyClass.kt: (16, 19) Unresolved reference: functionName
[ERROR] MyClass.kt: (19, 28) Unresolved reference: SOME_FIELD
... etc.
Maven cannot resolve artifact that were installed/deployed with a version from parameter.
In order for your artifacts to be resolvable after build, you will have to use flatten-maven-plugin so you get an effective pom which will be resolvable by maven.
You can see an example on how to do this in maven-ci-friendly

How to include opencv in an intellij sub module (maven)

I'm using IntelliJ IDE with maven. I have a project (main module) with a parent pom, that includes 2 sub modules, each with their own pom.
<!-- main pom module part -->
<packaging>pom</packaging>
<modules>
<module>ModuleA</module>
<module>ModuleB</module>
</modules>
<!-- example for sub module pom -->
<parent>
<artifactId>main-module</artifactId>
<groupId>my.main.module</groupId>
<version>0.5.0</version>
</parent>
Image ModuleA includes the OpenCV Java wrapper and ModuleB is an executable java program (having the main class) using ModuleA.
The compiling works fine, but when I run ModuleB with having set the library path in the launcher, I'll get the following error for ModuleA:
java.lang.NoClassDefFoundError: org/opencv/core/Core
Any suggestions how to fix this?
Ok, I found a solution my self. The problem was, that the opencv java wrapper was included with a system path. Now I use the maven install plugin within the validate live cycle step instead.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>3.3.0</version>
<packaging>jar</packaging>
<file>${project.basedir}/../lib/opencv/opencv-330.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Works fine for me, but was not the way I wanted it to be... The system-path type dependency seems to be buggy in maven.
Try to add the following dependency to your ModuleA:
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-7</version>
</dependency>

How can i correctly configure maven multimodule project

I'm trying to configure my multimodule maven project like this :
Parent Project
web
dao
core
<modules>
<module>../dao</module>
<module>../core</module>
<module>../web</module>
</modules>
My "web" module have one dependency to my "core" project and my core project to my "dao" project.
For example, my web pom contain :
<dependency>
<groupId>com.groupid</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
When I build my parent project all build work fine but there is a very strange dependency copy. In fact, my parent project contains all child dependencies
For example, joda-time is in my web pom.
When I try to uncompress my web.war, it contains my core-jar-with-depency.jar and all the dependency.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<skipAssembly>false</skipAssembly>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
How can I configure my parent project to disable dependency inclusion?
Any specific reason for using classifier jar-with-dependencies? Looking at your need, I think you can do this ---
dao --> module with packaging jar
core --> module with packaging jar & dependency of dao without any classifier.
web --> module with packagingc war & dependency of core without any classifier.
Eventually your web war file will 'transitively' get core & dao & all its dependencies. See if this helps.
--- Update:
I just created one sample project with core module & web module in my GIT hub.
https://github.com/Ravikharatmal/MyGitRepo/tree/master/MultiModuleMavenProject
Web is dependent on Core. Core has a dependency on third party library commons-lang. When I do 'mvn install', you can see that the final web.war file has core.jar as well as commons-lang.jar i.e. transitive dependency of core.jar.
https://github.com/Ravikharatmal/MyGitRepo/tree/master/MultiModuleMavenProject/web/target/web-0.0.1-SNAPSHOT/WEB-INF/lib
I hope this is what you are looking for.

Generate test-jar of unit testing project and use in other maven projects

I created a unit-test maven project as a base project which other project can extend and use Described here. Here is the pom.xml -
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.6.4</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This generates two jars in target -
unit-test-0.0.1-SNAPSHOT.jar
unit-test-0.0.1-SNAPSHOT-tests.jar
Now, I have some spring boot microservice projects say service-a and service-b which are using another maven project say super-service as dependency. service-a and service-b are using super-service as following declaration in respective services pom.xml -
<dependency>
<groupId>com.super.service</groupId>
<artifactId>super-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
I have written unit test for classes in super-service by using above unit-test maven project which is working fine. The pom.xml of super-service is -
<dependency>
<groupId>com.unit-test</groupId>
<artifactId>unit-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
But service-a and service-b is not working in same manner. I thought above dependency should get resolved through the base i.e. super-service but it's not and test gets fail. Then I have tried to repeat the same dependency declaration in each services pom.xml but still maven test gets fail.
I tried the other way described in that URL which tells to move source files from src/test/java to src/main/java but that also worked only for super-service project and not for service-a and service-b.
You're doing the wrong thing. You should not run the unit-tests of your dependencies -- if you don't think it can pass its tests you shouldn't be using it.
Only use a dependency when you have evidence that it passed its tests. This is controlled by your CI process which should only put successfully tested binaries in your binary repository. For most people this means that Jenkins should run your tests and if they all pass only then should it put the binary in nexus for you to depend on it. As it does the release to nexus the CI process, through maven, should also update the version number from a snapshot to a release version so that you're never depending on snapshots.

Specify both jar and test-jar types in Maven dependencies

I have a project called "commons" that contains common includes for both runtime and test.
In the main project I added a dependency for commons:
<dependency>
<groupId>com.alexb</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
However the test common files are not included. So I added :
<dependency>
<groupId>com.alexb</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
However when type is test-jar, the runtime is not included.
Unfortunatelly, it seems I cannot include both:
<type>jar,test-jar</type>
What can I do to include both?
As #khmarbaise mentioned in the comments you should separate your test-jar part project.
I presume you have in the commons pom.xml something like this which generates common test-jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
The problem with this approach is that you don't get the transitive test-scoped dependencies automatically.
Check this link for more details:
https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html

Categories

Resources