Generating WAR files from a multiproject Maven configuration
-
28-09-2019 - |
Question
I have a Maven project, with 4 components Web, Persistence, Common and Other.
The relevant stuff off my POM files:
Parent POM:
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>components/TestWeb</module>
<module>components/TestOther</module>
<module>components/TestPersistence</module>
<module>components/TestCommon</module>
</modules>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<dependentWarExcludes>
**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**
</dependentWarExcludes>
</configuration>
</plugin>
</plugins>
</build>
common pom:
<modelVersion>4.0.0</modelVersion>
<artifactId>test-common</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
persistence pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
web pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestWeb</name>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Maybe I broke something in the copy&paste, but the XML is valid.
- when I run mvn package, the project WAR file isn't created, but all components packages are created and well formed.
- if, then, I run
mvn war:war
, the WAR file is generated empty.
How can fix this?
Solution
Having plugins in the root project does not work. You can configure the plugins here, like this
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
but they still have to be referenced in the subprojects to be active (except for plugins that are part of the default process, like maven-compiler-plugin and maven-resource-plugin).
So either you move your war-plugin configuration to pluginManagement
in the root project and include
<build>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<execution>
<goals>...</goals>
</execution>
</plugin>
</build>
in your war project or you move the entire configuration into the war.
Additional note: Make sure the order of the modules in the root pom is aligned to the dependency relations between the projects (In your case just reverse the order).
OTHER TIPS
I created a similar project structure and pasted the POMs you provided but couldn't reproduce the problem. Running mvn package
from the aggregating pom just works as expected:
$ mvn package [INFO] Scanning for projects... ... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] ------------------------------------------------------------------------ [INFO] Unnamed - com.test:test:pom:0.0.1-SNAPSHOT ............ SUCCESS [5.819s] [INFO] Unnamed - com.test:test-common:jar:0.0.1-SNAPSHOT ..... SUCCESS [3.343s] [INFO] Unnamed - com.test:test-persistence:jar:0.0.1-SNAPSHOT SUCCESS [0.136s] [INFO] Unnamed - com.test:test-other:jar:0.0.1-SNAPSHOT ...... SUCCESS [0.079s] [INFO] Unnamed - com.test:test-web:war:0.0.1-SNAPSHOT ........ SUCCESS [1.899s] [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ ...
And produces the following result:
$ tree . . ├── components │ ├── TestCommon │ │ ├── pom.xml │ │ ├── ... │ │ └── target │ │ ├── maven-archiver │ │ │ └── pom.properties │ │ └── test-common-0.0.1-SNAPSHOT.jar │ ├── TestOther │ │ ├── pom.xml │ │ ├── ... │ │ └── target │ │ ├── maven-archiver │ │ │ └── pom.properties │ │ └── test-other-0.0.1-SNAPSHOT.jar │ ├── TestPersistence │ │ ├── pom.xml │ │ ├── ... │ │ └── target │ │ ├── maven-archiver │ │ │ └── pom.properties │ │ └── test-persistence-0.0.1-SNAPSHOT.jar │ └── TestWeb │ ├── pom.xml │ ├── src │ │ └── main │ │ └── webapp │ │ ├── index.jsp │ │ └── WEB-INF │ │ └── web.xml │ └── target │ ├── maven-archiver │ │ └── pom.properties │ ├── test-web-0.0.1-SNAPSHOT │ │ ├── index.jsp │ │ ├── META-INF │ │ └── WEB-INF │ │ ├── classes │ │ ├── lib │ │ │ ├── test-common-0.0.1-SNAPSHOT.jar │ │ │ ├── test-other-0.0.1-SNAPSHOT.jar │ │ │ └── test-persistence-0.0.1-SNAPSHOT.jar │ │ └── web.xml │ └── test-web-0.0.1-SNAPSHOT.war └── pom.xml
So your problem must have something to do with your WAR project itself, its structure or something like this. Please show its structure and the output of Maven when running war:war
on it.
By the way, here is how a POM should typically look like in a multi-modules build:
<project>
<modelVersion>4.0.0</modelVersion>
<!--groupId>com.test</groupId--> <!-- unnecessary, you inherit it -->
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<!--version>0.0.1-SNAPSHOT</version--> <!-- unnecessary, you inherit it -->
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId> <!-- DRY, use built-in properties -->
<artifactId>test-common</artifactId>
<version>${project.version}</version> <!-- DRY, use built-in properties -->
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>test-persistence</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>test-other</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Another thing, the war plugin configuration from the parent project is inherited (you can check that by running help:effective-pom
in the web module) but it would make sense to configure it in the web module itself.