I have a project-test which contains all classes that are used in JUnit Tests of others projects (e.g builders tests). In pom.xml of project-test I added Mockito and Junit.
<?xml version="1.0" encoding="UTF-8"?>
<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>br.com.company</groupId>
<artifactId>project-test</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Note that many projects needs to use project-test to import JUnit and Mockito dependency. Foo project has a pom.xml with project-test dependency:
<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>br.com.company</groupId>
<artifactId>foo</artifactId>
<packaging>war</packaging>
<name>foo</name>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<!-- <defaultGoal>install</defaultGoal> -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>3.5.1</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<!-- Nexus config -->
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>br.com.company</groupId>
<artifactId>project-test</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
It needs to import some packages like:
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
I executed mvn clean install and the .jar inside /home/danielamorais/.m2/repository/br/com/company/project-test/1.0-SNAPSHOT exists. Why I can't import Junit and Mockito in my Foo Project?
Error:(8, 17) java: package org.junit does not exist
Those are test scope dependencies and will only be applicable when running the test.
I hope you are testing this by executing the mvn test.
ex mvn -Dtest=YourTestCase test
Like JB Nizet and Nrj mentioned, it's a matter of scopes. Check Maven documentation on the subject.
I just wanted to add that instead of creating a whole new project for grouping your common dependencies, a simpler and common practice is to create a parent POM then your other POMs can inherit from it. Read more about it here, there are a couple examples.
As an artifact declare as dependency another artifact, dependencies with the test scope are not inherited in the declaring artifact.
If what you want is defining common dependencies (and same common configuration) for multiple artifacts, use the feature designed to : the parent pom.
Define a parent pom that holds common configuration and make your two artifacts some child modules of it.
You may also make the parent pom a aggregator/multi-module pom if required.
Parent pom
<?xml version="1.0" encoding="UTF-8"?>
<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>br.com.company</groupId>
<artifactId>foo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
project-test (jar)
<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>
<parent>
<groupId>br.com.company</groupId>
<artifactId>foo-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>project-test</artifactId>
</project>
foo (war app)
<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>
<parent>
<groupId>br.com.company</groupId>
<artifactId>foo-parent</artifactId>
<version>1.0</version>
</parent>
<packaging>war</packaging>
<artifactId>foo</artifactId>
</project>
Related
I've tried to find a proper solution to my question, but it looks like it is similar to the following one maven assembly include the current project jar in the final zip/tar.
The idea of a project is to have one parent pom and a couple of child ones.
I would like to have 'jar-with-dependencies' for each of child when I will execute 'mvn assembly:single' command from the root level.
So, what I have got so far:
If I execute mvn package and mvn assembly:single one by one, then first will be completed successfully and the second one with a warning that child project was not included. Since my module has not been included, I am not able to launch the target.
If I execute mvn package assembly:single, then required jar with all the dependencies will be created and I am able to launch the target.
I afraid that I have missed a configuration in one of my pom.xml. I will appreciate if someone could help me with that. I am adding a link on GitHub repository with this example.
btw, I'm using maven-assembly-plugin version 3.1.0
Thank you in advance, and I guess, I will need to buy a really good book about Maven.
The key point is disabling assembly execution for parent project: it can be achieved combining <skipAssembly> option, profiles section and properties section.
With the following pom files, it works fine.
Parent pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.slemarchand.samples.jarwithdependencies</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<skip.assembly>true</skip.assembly>
</properties>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<skipAssembly>${skip.assembly}</skipAssembly>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>jar</id>
<activation>
<file>
<exists>${basedir}/src/main/java</exists>
</file>
</activation>
<properties>
<skip.assembly>false</skip.assembly>
</properties>
</profile>
</profiles>
</project>
Child 1 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.slemarchand.samples.jarwithdependencies</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>child1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>child1</name>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Child 2 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.slemarchand.samples.jarwithdependencies</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>child2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>child2</name>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
</build>
</project>
You can find the sample project here: https://gitlab.com/slemarchand/jar-with-dependencies-multi-module-sample.
Thanks for the answers.
It turns out that if was misconfiguration in child pom.xml.
I didn't add definition of the plugin under the plugins section:
<build>
<plugins>
HERE
<plugins>
</build>
So it was properly described under <pluginManagement> and it blinds me...
when I start learning spring boot,I made a project springboot-demo as the root, an three modules admin-service、common-service and dao.
this is root pom:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<packaging>pom</packaging>
<name>springboot-demo</name>
<modules>
<module>admin-service</module>
<module>common-service</module>
<module>dao</module>
</modules>
<groupId>cc.xiaoerbi</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>
</project>
this is the admin pom:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>cc.xiaoerbi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>admin-service</name>
<artifactId>admin-service</artifactId>
<dependencies>
<dependency>
<groupId>cc.xiaoerbi</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and this is the dao pom:
<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">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>cc.xiaoerbi</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>dao</artifactId>
<dependencies>
<!-- Spring Boot Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
I package admin war failed
told:
[WARNING] The POM for cc.xiaoerbi:dao:jar:1.0-SNAPSHOT is missing, no dependency information available
I've written some similar code, and it work. I don't know what's wrong with this code.
change your module so that you compile your dependecies before using them.
<modules>
<module>dao</module>
<module>admin-service</module>
<module>common-service</module>
</modules>
The folder names that contain the dao, admin-service, and common-service pom.xml shold be in the root folder and they should match the modules names in the root pom.xml. Also, you should run the "mvn clean install" compile from the root directory and not the admin directory. If you compile from admin, it will not compile dao first. It will try to pull in from the repo when it cannot find a resource in the ~/.m2/repositoy local cache.
Add the
<relativePath>../pom.xml</relativePath>
to the admin-service and common-service.
They talk about it in Project Inheritance section example 2 on https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
I just got in issue related to Maven and Dependent JAR version. I create following project to analyze issue.
I created App_1.jar which is using spring version 4.2.9.
I created App_2.jar which is using spring version 4.3.4.
I created App_3.jar which is using spring version 4.3.6.
I created App_Main.war which will App_1.jar, App_2.jar, App_3.jar.
According to maven if you use different version it will use the latest one but in my case it using the spring version of jar which i included first which is App_1.jar and its version is 4.2.9.
Here is the code.
**App_1 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ksh</groupId>
<artifactId>App_1</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>App_1</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.9.RELEASE</version>
</dependency>
</dependencies>
</project>
**App_2 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ksh</groupId>
<artifactId>App_2</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>App_2</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
</project>
**App_3 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ksh</groupId>
<artifactId>App_3</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>App_3</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
</dependencies>
</project>
App_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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>App_Main</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>App_Main Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.ksh</groupId>
<artifactId>App_1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ksh</groupId>
<artifactId>App_2</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ksh</groupId>
<artifactId>App_3</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<finalName>App_Main</finalName>
</build>
</project>
Where did you read maven use the latest one?
For transitives dependencies Maven uses a "nearest-wins" strategy to resolves version conflicts, and that means it will use the version of the closest dependency to your project in the tree of dependencies.
Maven transitive dependencies
One possible solution is use the <dependencyManagement> to resolve your conflicts.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>spring_context_version_you_want_to_use</version>
</dependency>
</dependencies>
</dependencyManagement>
Maven dependency managament
You can check your tree of dependencies with the command:
mvn dependency:tree -Dverbose -Dincludes=your-jar
Resolving conflicts using the dependency tree
I created this example POM file so that I do not need the dependency versions replicated in each child project. My requirement is that the thirdparty version need not be provided for each project. Each project should inherit the library versions from the bom file. I created the example based on the documentation of Maven dependencies. My maven version is Apache Maven 3.2.5
I am getting the error below
[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project org.test.testapp:test-app:1.0-SNAPSHOT (/Users/skoya/workspace/test-app/pom.xml) has 2 errors
[ERROR] 'dependencies.dependency.version' for junit:junit:jar is missing. # line 22, column 17
[ERROR] 'dependencies.dependency.version' for commons-cli:commons-cli:jar is missing. # line 26, column 17
root bom pom 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>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test-dependencies-bom</name>
<url>http://myorg.org</url>
<properties>
<commons-cli.version>1.3</commons-cli.version>
<junit.version>3.8.1</junit.version>
</properties>
<modules>
<module>test-dependencies</module>
</modules>
</project>
test-dependencies/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>org.test</groupId>
<artifactId>test-parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test-parent-pom</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<prerequisites>
<maven>3.0.0</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
Maven project files referencing the bom 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test.testapp</groupId>
<artifactId>test-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-app</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
</dependencies>
</project>
There are 2 issues:
For importing of dependencies, the imported pom project (test-dependencies/pom.xml) should define the dependencies to import in a <dependencyManagement> section, not just <dependencies> as was done in your original sample.
The project that imports that pom needs to import the project that declares the dependencies (test-dependencies/pom.xml), not the parent pom.xml.
I fixed this by making the following changes.
test-dependencies/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>org.test</groupId>
<artifactId>test-parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test-parent-pom</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.test</groupId>
<artifactId>bom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<prerequisites>
<maven>3.0.0</maven>
</prerequisites>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Maven project files referencing the bom 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test.testapp</groupId>
<artifactId>test-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-app</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.test</groupId>
<artifactId>test-parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
</dependencies>
</project>
After that, I was able to build and verify that the project importing the pom was bringing in the expected dependencies:
mvn dependency:tree
...
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # test-app ---
[INFO] org.test.testapp:test-app:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:compile
[INFO] \- commons-cli:commons-cli:jar:1.3:compile
Note that it still successfully pulled the version numbers as defined in properties from the parent pom.xml. This a perfectly valid way to structure the project. It just needed the 2 adjustments I described.
There is a Maven project that used to have JAR packaging type. Now the project has evolved, and it is required that it be built as EAR, including the JAR proper and its dependencies altogether.
Does it necessarily mean I have to introduce the second POM with EAR packaging type? Is it possible to build JAR+EAR from a single Maven project? The reason behind that is that I don't really want to radically reorganize my repository structure.
If not possible, I guess I'd need to somehow reorganize project structure to make use of Maven modules. What (sub)module structure would you recommend in that case?
You will need 3 poms, here is a working example;
pom 1 is the parent
<?xml version="1.0" encoding="UTF-8"?>
<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.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ear-example</name>
<modules>
<module>example-jar</module>
<module>example-ear</module>
</modules>
</project>
pom 2 is the jar or war
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>example-jar</artifactId>
<packaging>jar</packaging>
<name>com.greg</name>
<url>http://maven.apache.org</url>
<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>
</dependencies>
</project>
pom 3 is the ear which has a dependency to the jar/war and uses the ear plugin
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>example-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>example-jar</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
I think you can use maven war plugin to generate a single war file every time you build the project
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.3.2</version>
<!-- configuring the ear plugin -->
<configuration>
<modules>
<!-- all your modles -->
</modules>
</configuration>
</plugin>
</plugins>