|
Maven clearly differentiate "productive code" in each module src/main/* from unit test code in src/test/*. In a typical multi-module projects like the one below: componentA
|
|-moduleA
| /src/main/java
| /src/main/resources
| /src/test/java
| /src/test/resources
|-moduleB
| /src/main/java
| /src/main/resources
| /src/test/java
| /src/test/resources
Note1: Eclipse do not support multi module project as only one level of code sharing is allowed. So we have
3 eclipse projects/maven projects with a pom.xml:
- componentA with a packaging pom, and 2 module aggregated
- moduleA, parent is ../componentA/pom.xml
- moduleB parent is ../componentA/pom.xml
Note2: eclipse has only one Class Loader, meaning that code in /src/main/java and /src/test/java is also exported
as dependencies between modules, while in Maven, code is not shared!
So as default by adding a dependencies in moduleB/pom.xml to moduleA, you'll only inherit moduleA src/main/java
and src/main/resources
<dependency>
<groupId>com.waltercedric.maven</groupId>
<groupId>modulesA</groupId>
</dependency>
But what if you have some test API classes in modulesA? trying to add also moduleA in scope test wont help you any further:
<dependency>
<groupId>com.waltercedric.maven</groupId>
<artifactId>modulesA</artifactId>
</dependency>
<dependency> <!-- do not work! -->
<groupId>com.waltercedric.maven</groupId>
<artifactId>modulesA</artifactId>
<scope>test</scope>
</dependency>
This is exactly where artifact classifier may help you, but lets first look at the artifact naming convention.
artifact name = {name/artifactId}-{version}-{classifier}.{extension}
Maven is also introducing some conventions:
-> name is most of the time the artifactId
-> version being the version number of the artifact, simply don't use something containing SNAPSHOTS in it as it is considered being non stable by some plugin (maven-release-plugin for example) -> classifier is either
- empty like for ex: jaxb-1.2.jar and then it will contains the binary package of the library jaxb
- source like for ex: jaxb-1.2-source.jar
- javadoc like for ex: jaxb-1.2-javadoc.jar
- but it can be anything! as it just classify an artifact, but don't use it in place where an artifact could
have been use, a classifier proxy/stub is not recommended: make a module of it instead. You must see
classifier more as a way to categorize artifact in a module.
You'll find a lot of artifact across Internet not following these guidelines, this is not an issue as Maven repositories are
able to search for pom.properties or project.xml or pom.xml in jar if they exist and use the right <groupId> and
<artifactId>
Back to code reuse of test classes across modules, the trick is to tell maven to make a jar of every module test code so
you can depend on it in others modules.
a normal build of componentA, would create:
# /componentA/mvn clean install
/.m2/repository/com/waltercedric/maven/moduleA/0.0.1.SNAPSHOTS/moduleA-0.0.1.SNAPSHOTS.jar
/.m2/repository/com/waltercedric/maven/moduleB/0.0.1.SNAPSHOTS/moduleB-0.0.1.SNAPSHOTS.jar
where by just adding to componentA/pom.xml the following inside the <build></build>:
<!-- this create jar file of code from src/test/java so modules with tests can share code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
# /componentA/mvn clean install
/.m2/repository/com/waltercedric/maven/moduleA/0.0.1.SNAPSHOTS/moduleA-0.0.1.SNAPSHOTS.jar
/.m2/repository/com/waltercedric/maven/moduleA/0.0.1.SNAPSHOTS/moduleA-0.0.1.SNAPSHOTS-tests.jar
/.m2/repository/com/waltercedric/maven/moduleB/0.0.1.SNAPSHOTS/moduleB-0.0.1.SNAPSHOTS.jar
/.m2/repository/com/waltercedric/maven/moduleB/0.0.1.SNAPSHOTS/moduleB-0.0.1.SNAPSHOTS-tests.jar
Maven now create new artifacts moduleA-0.0.1.SNAPSHOTS-tests.jar!
So you can now add a dependencies in moduleB/pom.xml to moduleA in scope test with a tests classifier
<dependency> <!-- reuse src/main/java code from moduleA, normal dependencies --> <groupId>com.waltercedric.maven</groupId>
<artifactId>modulesA</artifactId>
</dependency>
<dependency> <!-- reuse src/test/java code from moduleA! -->
<groupId>com.waltercedric.maven</groupId>
<artifactId>modulesA</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
You can now share test code across module. Related Posts
-
What you will learn in this small post How to create JAXB proxies at build time using maven-jaxb2-plugin in a continuous build environment (TeamCity / Bamboo) How to generate from an XSD file (XML-Schema-Definitions) Java code. Requirements We will use JAXB2 (see JSR 222 and JAXB 2.x). We use Maven 2.2.1, the latest available v 119 days ago
-
The Apache Felix Maven SCR Plugin is a great tool to ease the development of OSGi components and services. Components and services are defined through annotations and the plugin creates the necessary descriptors for the OSGi Declarative Services, Config Admin and Metatype services. Starting with version 1.4.0 of the plugin, OSGi Declar 135 days ago
-
On larger projects, additional dependencies often tend to creep into a POM as the number of dependencies grow. As dependencies change, you are often left with dependencies that are not being used, and just as often, you may forget to declare explicit dependencies for libraries you require. Because Maven 2.x includes transitive dependencies in 135 days ago
-
What you will learn in this Maven How To How to generate JAXWS proxies stub against a local WSDL, remote WSDL How to compile your maven project or module against a specific version of Java (here 1.6) using Maven Compiler Plugin How to attach source code of your project with the binary artifact using Maven Sources Plugin How to de 183 days ago
-
'Integration testing' (sometimes called Integration and Testing, abbreviated I&T) is the activity of software testing in which individual software modules are combined and tested as a group. It occurs after unit testing and before system testing. Integration testing takes as its input modules that have been unit tested, groups them in 187 days ago
-
Acunetix Web Vulnerability Scanner (WVS) is an automated web application security testing tool that audits your web applications by checking for exploitable hacking vulnerabilities. Automated scans may be supplemented and cross-checked with the variety of manual tools to allow for comprehensive web site and web application penetration testing. Ac 188 days ago
-
Static analysis is in the verification of properties of software used in safety-critical computer systems and locating potentially vulnerable/buggy code. it is desirable to make your build fails at compile/test phases to detect faults earlier. Thanks to JSFUnit and Maven, you’ll be able to plug a JSF checker in your build with no effort. JSFUnit 188 days ago
-
Depending on your project requirements/number of customers, you may have to support different target environment. This article will help you to make your Maven build a bit more portable in that sense. Maven can help you avoiding having stage dependent data across all your Maven projects/ modules very easily thanks to resources filtering. Let 188 days ago
-
How to add dependencies graph to multi module projects. With this Maven plugin, you•ll be able to visualize Maven modules interdependencies and dependencies in any scope (compile, text, provided, system, runtime) depgraph:depgraph Can be used to draw a dependency graph from the project, the mojo is executed in. It traverses all dependenci 197 days ago
-
These things have disturbed us (The developer Team at Innoveo.com) a lot in the past months. We did solve them recently, and I would like to publish them now here to help more people Someone create a new maven module, after updating from SVN the module is not visible as a separate project. Scenario: User A checks out a maven project from SV 238 days ago
relatedArticles
|