Can't run maven tests that include many projects - java

I have many java projects, each with its maven build. They all install fine.
In addition, I have many JUnit tests. These tests may depend on other projects, i.e. a test from Project A may import a class from Project B.
My projects are not structered good (all of the classes are just in src directory and not src/main/java). So when I run mvn clean test I get No sources to compile. If I structure it good by moving all of the main classes to src/main/java and all the tests to src/test/java I keep getting cannot find symbol (with a reference to an import of a class in a different project).
I tried defining <testSourceDirectory>, used many different plugins and profiles and also tried lowering my java configuration level to 1.7, all to no avail.
I even tried defining a TestProject that has a simple POM with many modules (either with only <modules>, or defining <dependencies> or both), but even in this - either I get No sources to compile, cannot find symbol or even package x.y does not exist (or it compiles but trying to include it in other projects does nothing).
Can I run a test that involves classes from other other projects? How?

Maybe you can have and use Test dependencies. I'll explain how i achieve that, hope it's usefull for your project.
I've got a project with many subprojects (named projectA, projectB, etc.) each one with its own Tests. Also one of the projects named 'core' with jar packaging that contains common classes. They look like the following structure:
Main
|- core (jar)
|- projectA (war)
|- projectB (war)
All tests on the diferent projects used common code that finally were placed on 'core' project test clases.
This is how we manage the dependencies on maven 3.1.1.
Only for reference Main is also a project with its own pom.xml
Main (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.mycompany.myproject</groupId>
<artifactId>MyArtifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
</properties>
<repositories>
</repositories>
<modules>
<module>core</module>
<module>projectA</module>
<module>projectB</module>
</modules>
...
</project>
On core project pom.xml we put:
<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>core</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.mycompany.myproject</groupId>
<artifactId>MyArtifact</artifactId>
<version>1.0</version>
</parent>
<build>
<pluginManagement>
<plugins>
<!-- Common test classes jar creation -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
And on the rest of the projects pom.xml we put the dependency with this jar:
<dependencies>
<!-- We put dependency with core.jar an test-core.jar -->
<dependency>
<groupId>com.mycompany.myproject</groupId>
<artifactId>core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.mycompany.myproject</groupId>
<artifactId>core</artifactId>
<version>1.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Eclipse warns about this maven dependency but it works fin when running Test from the command line with maven.
Hope this helps.

Related

Maven (eclipse) use shaded jar instead of original jar

I have a project, where the packages will be relocated (shaded jar)
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>
<artifactId>artifact-child</artifactId>
<name>artifact-child</name>
<parent>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>${revision}</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!-- <shadedArtifactAttached>true</shadedArtifactAttached> -->
<relocations>
<reloaction>
<pattern>example</pattern>
<shadedPattern>${super-package}.example</shadedPattern>
</reloaction>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And I have a second project, which need the shaded jar at runtime.
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>testversion</groupId>
<artifactId>testv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testv</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>artifact-child</artifactId>
<version>0.0.1</version>
<classifier>shaded</classifier>
</dependency>
</dependencies>
</project>
My question:
Eclipse find the other project in his workspace and use directly the source code.
For example: I must write import example.myclass instead of import org.example.myclass.
In some cases, this can be a problem. Is it possible to say, what maven or eclipse should use the "shaded jar" instead of the original source code?
Must I create a online repository, so maven can only download the shaded jar?
I found two other stackoverflow posts (with no result):
Maven Eclipse multi-module shaded dependency
How to install shaded jar instead of the original jar
SOlVED:
My Mistakes:
1. version of the parent must declare directly not via properties
2. Forgot to run "Maven Install"
Solution:
Maven run without errors, but Eclipse use the open project and not the shaded jar.
Found the solution here: How can I download a single raw file from a private github repo using the command line? .
Open the properties of the Project. Under the Tap Maven, remove the check from "Resolve dependencies from Workspace projects"
If the shaded jar is your dependendy, you just reference the classes/packages as they are defined in the shaded jar.
If Eclipse shows an error, Eclipse is wrong (which is often the case). First of all, try to build your project with something like clean verify. Then you see whether you really have errors (or if Eclipse made them up).
If Eclipse errors bother you, try ALT+F5.

Intellij maven module inheritence

I have gone through the documentation, but still not able to achieve what I am trying to. The requirement is very simple. I have two maven projects. ProjectA and ProjectB. ProjectB requires to reuse some common configs and code from ProjectA. I don't want to just copy and paste them as this is will require to updates if anything changes. So, what are the options now? How can I achieve this?
I don't think there is a silver bullet for this but we use combination of these two aproaches:
Multi-module project (best if projects are connected and you want to change common parts a lot): https://maven.apache.org/guides/mini/guide-multiple-modules.html
Extract parts you want to reuse from projectA and make it a standalone artifact which you publish to maven repo (local/private/public - depending on your use case).
You might have two options.
Use your ProjectA has parent of your projectB
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<parent>
<groupId>com.example</groupId>
<artifactId>projectA</artifactId>
<version>1.0</version>
</parent>
<artifactId>projectB</artifactId>
Thus, your projectB will inherit from the first one, with all its dependencies build / dependency management.
Use your ProjectA has dependency of your projectB
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<artifactId>projectB</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>projectA</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
In this case, projectB wil inherit from projectA all the sources and dependencies.
I created a multi module maven project but it was always compiling that the packages not found. Eventually, I figured out the issue here. Maven Multi-module dependency package not found
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>

Maven error when pom.xml is packaged an ejb, but goes away when packaged as jar

When I package my pom.xml as an EJB I get an error that I do not understand. This error shows up if I touch the package in any way, to include Ctrl+S and Maven update. The error goes away if I change the packaging back to jar.
Code:
<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>
<parent>
<groupId>com.myorg.buildparent</groupId>
<artifactId>BuildParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.myorg.myEjbPackage</groupId>
<artifactId>MyEjbPackage</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ejb</packaging>
</project>
By changing
<packaging>ejb</packaging>
to
<packaging>jar</packaging>
The error will go away.
Error string for reference:
'Building workspace' has encountered a problem.
Errors occurred during the build.
Errors running builder 'Maven Project Builder' on project 'MyEjbPackage'.
org.codehaus.plexus.archiver.ArchiverException
Even though I can remove the error, I still feel like it should be packaged as an ejb. Does this make sense to anyone? Does my EJB package need to be packaged as a EJB in the pom? I should admit that my skills with Maven are around that of a novice/low intermediate.
Try adding this configuration in your POM. The EJB plugin (which is bound to the lifecycle by default when you select ejb packaging type) doesn't use the same version of the archiver that the jar/war/ear plugins do.
There is a JIRA issue for updating the dependency versions, however it has not been worked yet.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>${maven.ejb.plugin.version}</version>
<dependencies>
<!-- Use the same archiver as the other [j/w/e]-ar plugins -->
<!-- See http://jira.codehaus.org/browse/MEJB-52 -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
Update
MEJB-52 is fixed in maven-ejb-plugin version 2.4 and later. Consider updating the plugin version to the latest before adding this configuration.

Maven generate jar dependencies then war

I'm using m2e Maven Plugin for Eclipse. I'm having 5 eclipse projects. A web application project and then 4 projects as jars dependencies for my web application.
I would like to know how can I package jars before including them in the WAR using "mvn clean install" on war project.
Here's my 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>dispatcher</groupId>
<artifactId>dispatcher</artifactId>
<version>4.0</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>referentiel</groupId>
<artifactId>referentiel</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>mailTemplates</groupId>
<artifactId>mailTemplates</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>qualityTool</groupId>
<artifactId>qualityTool</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>tools</groupId>
<artifactId>tools</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
...
..
.
</dependencies>
</project>
Thank you in advance.
The answer of #Jigar Joshi is good but i thing you need a view of structure which can help you to understand quickly what we mean.
I. Create a top level maven module (parent of war and jars)
You habe already the 5 moduls that you need. Now create a new Maven project as parent which must contain only a pom.xml file.
parent project pom
<project...>
<groupId>groupId</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Define parent pom </name>
<!-- modul -->
<project>
II. Put your jar projects first as modul and at the end the war project. If you have another dependencies in the jar projects you may also try to order them consequently.
parent project pom
<modules>
<module>referentiel</module> <!-- jar -->
<module>mailTemplates</module> <!-- jar -->
<module>qualityTool</module> <!-- jar -->
<module>tools</module> <!-- jar -->
<module>dispatcher</module> <!-- war-->
</modules>
III. in all other project put the parent reference into the poms
<parent>
<groupId>groupId</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
IV. Now you can go to inside the new created parent project and run from there
mvn clean install
Either create a top level maven module (parent of war and jars) and execute mvn clean install
---pom.xml
|
|dispatcher---pom.xml (war)
|qualityTool----pom.xml (jar)
|mailTemplates----pom.xml (jar)
|referentiel----pom.xml (jar)
|tools----pom.xml (jar)
or use --also-make command line option to make dependencies as well

communicate between wars and jar inside ear

I'm new to creating ear and communicaton between wars and jar...
I was having two wars having completely independent functionality.
Now I've to create a Ear, where two application have to work on same functionality,
which is enclosed in a jar.But requirement is I must not include the jar in Pom.xml of both but make use of that jar, where all 3 are under single ear.is this is possible?
I've tested Ear with 2 independent wars and it's working fine now how to achieve above I'm not getting this.
I'm using Maven with Jboss7.1.1.
I went through links like MessageHandler in JAR/WAR/EAR , https://stackoverflow.com/questions/1796255/tell-me-a-clear-differnece-between-ear-war-and-jar but got no idea about above problem.
You can put multiple wars and jars into a ear and they can share the same classloader. This means that all the classes are accessible from all the jars/wars. i.e. it is as if all the classes / resouces are in one archive without the sub-packaging.
I am assuming this is what you mean by "communication between wars and jars".
EDIT: check Making an EAR with Maven for an example of the pom.xml for building an ear. In the example, there is one jar and one war but you could have any number of wars / jars.
Hi got the solution >> here it is.. this is a pom.xml of ear 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>Test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ear</packaging>
<dependencies>
<!--Dependency for jar-->
<dependency>
<groupId>com.jar</groupId>
<artifactId>com.jar</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
<!--Dependency for war1-->
<dependency>
<groupId>com.war2</groupId>
<artifactId>com.war2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<!--Dependency for war2-->
<dependency>
<groupId>com.war1</groupId>
<artifactId>com.war1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<finalName>Project</finalName>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<finalName>MyEarFile</finalName>
<version>5</version>
<modules>
<!--Webmodule for war1-->
<webModule>
<groupId>com.war1</groupId>
<artifactId>com.war1</artifactId>
<uri>war1.war</uri>
<bundleFileName>war1.war</bundleFileName>
</webModule>
<!--Webmodule for war2-->
<webModule>
<groupId>com.war2</groupId>
<artifactId>com.war2</artifactId>
<uri>war2.war</uri>
<bundleFileName>war2.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note:: groupId and artifactId metioned here must match with groupId and artifactId mentioned in the project's pom.xml.
Also dependency of jar must be present in this i.e. ear's pom.xml and not in both app's pom.xml.
At time of maven install it automatically refers to jar's contents..

Categories