How to structure a multi-modules Maven project to compile it at once? -
i have maven project multiple modules , sub-modules , want compile @ once, i.e. using 1 call "mvn clean install".
for basic project, following structure work:
. ├── modules │ ├── modulea │ │ └── pom.xml <--- module pom │ ├── moduleb │ │ └── pom.xml <--- module b pom │ └── pom.xml <--- super pom (at root of "modules" folder) └── pom.xml <--- aggregator pom
with aggregator being:
<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</groupid> <artifactid>aggregator</artifactid> <packaging>pom</packaging> <version>1.0.0-snapshot</version> <modules> <module>modules</module> <module>modules/modulea</module> <module>modules/moduleb</module> </modules> </project>
the super 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"> <modelversion>4.0.0</modelversion> <groupid>org.test</groupid> <artifactid>super-pom</artifactid> <packaging>pom</packaging> <version>1.0.0-snapshot</version> <dependencymanagement> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </dependencymanagement> </project>
module a's 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"> <modelversion>4.0.0</modelversion> <artifactid>modulea</artifactid> <parent> <groupid>org.test</groupid> <artifactid>super-pom</artifactid> <version>1.0.0-snapshot</version> </parent> </project>
module b similar.
when @ root of project, running "mvn clean install" command gives (after cleaning .m2/repository folder):
[...] [info] ------------------------------------------------------------------------ [info] reactor summary: [info] [info] super-pom .......................................... success [ 0.450 s] [info] modulea ............................................ success [ 1.746 s] [info] moduleb ............................................ success [ 0.029 s] [info] agregator .......................................... success [ 0.006 s] [info] ------------------------------------------------------------------------ [info] build success
now if want more complex (but still using 1 super pom), such as:
. ├── modules │ ├── lib1 │ │ ├── modulea1 │ │ │ └── pom.xml │ │ ├── moduleb1 │ │ │ └── pom.xml │ │ └── pom.xml <--- lib1 aggregator pom │ ├── lib2 │ │ ├── modulea2 │ │ │ └── pom.xml │ │ ├── moduleb2 │ │ │ └── pom.xml │ │ └── pom.xml <--- lib2 aggregator pom │ └── pom.xml <--- super pom └── pom.xml <--- aggregator pom
with root pom being:
<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</groupid> <artifactid>agregator</artifactid> <packaging>pom</packaging> <version>1.0.0-snapshot</version> <modules> <module>modules</module> <module>modules/lib1</module> <module>modules/lib2</module> </modules> </project>
lib1 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"> <modelversion>4.0.0</modelversion> <groupid>org.test</groupid> <artifactid>lib1-agregator</artifactid> <packaging>pom</packaging> <version>1.0.0-snapshot</version> <modules> <module>modulea1</module> <module>moduleb1</module> </modules> </project>
and, instance modulea1 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"> <modelversion>4.0.0</modelversion> <artifactid>modulea1</artifactid> <parent> <groupid>org.test</groupid> <artifactid>super-pom</artifactid> <version>1.0.0-snapshot</version> </parent> </project>
maven not manage resolve pom files (after cleaning .m2/repository folder):
[info] scanning projects... [error] [error] problems encountered while processing poms: [warning] 'parent.relativepath' of pom org.test:modulea1:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib1/modulea1/pom.xml) points @ org.test:lib1-agregator instead of org.test:super-pom, please verify project structure @ line 7, column 13 [fatal] non-resolvable parent pom org.test:modulea1:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 [warning] 'parent.relativepath' of pom org.test:moduleb1:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib1/moduleb1/pom.xml) points @ org.test:lib1-agregator instead of org.test:super-pom, please verify project structure @ line 7, column 13 [fatal] non-resolvable parent pom org.test:moduleb1:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 [warning] 'parent.relativepath' of pom org.test:modulea2:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib2/modulea2/pom.xml) points @ org.test:lib2-agregator instead of org.test:super-pom, please verify project structure @ line 7, column 13 [fatal] non-resolvable parent pom org.test:modulea2:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 [warning] 'parent.relativepath' of pom org.test:moduleb2:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib2/moduleb2/pom.xml) points @ org.test:lib2-agregator instead of org.test:super-pom, please verify project structure @ line 7, column 13 [fatal] non-resolvable parent pom org.test:moduleb2:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 @ [error] build not read 4 projects -> [help 1] [error] [error] project org.test:modulea1:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib1/modulea1/pom.xml) has 1 error [error] non-resolvable parent pom org.test:modulea1:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 -> [help 2] [error] [error] project org.test:moduleb1:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib1/moduleb1/pom.xml) has 1 error [error] non-resolvable parent pom org.test:moduleb1:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 -> [help 2] [error] [error] project org.test:modulea2:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib2/modulea2/pom.xml) has 1 error [error] non-resolvable parent pom org.test:modulea2:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 -> [help 2] [error] [error] project org.test:moduleb2:[unknown-version] (/users/ben/ideaprojects/complexmavenstructure/modules/lib2/moduleb2/pom.xml) has 1 error [error] non-resolvable parent pom org.test:moduleb2:[unknown-version]: not find artifact org.test:super-pom:pom:1.0.0-snapshot , 'parent.relativepath' points @ wrong local pom @ line 7, column 13 -> [help 2] [error] [error] see full stack trace of errors, re-run maven -e switch. [error] re-run maven using -x switch enable full debug logging. [error] [error] more information errors , possible solutions, please read following articles: [error] [help 1] http://cwiki.apache.org/confluence/display/maven/projectbuildingexception [error] [help 2] http://cwiki.apache.org/confluence/display/maven/unresolvablemodelexception
i need first build super pom:
cd modules/ mvn clean install
and compile:
[info] reactor summary: [info] [info] super-pom .......................................... success [ 0.271 s] [info] modulea1 ........................................... success [ 1.202 s] [info] moduleb1 ........................................... success [ 0.027 s] [info] lib1-agregator ..................................... success [ 0.006 s] [info] modulea2 ........................................... success [ 0.027 s] [info] moduleb2 ........................................... success [ 0.022 s] [info] lib2-agregator ..................................... success [ 0.007 s] [info] agregator .......................................... success [ 0.007 s] [info] ------------------------------------------------------------------------ [info] build success
how structure project make work without two-steps compilation?
[edit]: discussed in comments, adding relativepath of super pom modules poms make code compile 1 command line.
however, if want distribute lib1 developers, though super pom on maven repository, lib1 not compile without whole project structure. makes project less modular.
so let me start did, name things little bit different:
. ├── modules-root │ ├── modulea │ │ └── pom.xml <--- module pom │ ├── moduleb │ │ └── pom.xml <--- module b pom │ └── pom.xml <--- modules root └── pom.xml <--- project-root
so lets start project-root
this:
<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</groupid> <artifactid>project-root</artifactid> <packaging>pom</packaging> <version>1.0.0-snapshot</version> <dependencymanagement> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </dependencymanagement> <modules> <module>modules-root</module> </modules> </project>
so module parent
this. emphasis contains reference modulea
, moduleb
, inherit project-root
<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>org.test</groupid> <artifactid>super-pom</artifactid> <version>1.0.0-snapshot</version> </parent> <groupid>org.test.module</groupid> <artifactid>modules-root</artifactid> <packaging>pom</packaging> <modules> <module>modulea</module> <module>moduleb</module> </modules> </project>
so modulea
this. pay attentions inherit module-parent
(parent) 1 level above...
<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>org.test.module</groupid> <artifactid>module-parent</artifactid> <version>1.0.0-snapshot</version> </parent> <artifactid>modulea</artifactid> <packaging>..</packaging> <....other dependencies..> </project>
if use kind of structure can go project-root
level , do:
mvn clean install
furthermore afterwards can use things this:
mvn -pl modulea ...
to build modulea (but stay @ project-root
level...).
the module-parent
might wasted or superfluous in example, if more modules define there supplemental dependencies via dependencymanagement or may changing plugin configurations via pluginmanagement used in sub area (modulea, moduleb, ...). if project becomes larger more module-parents in parallel..which contain different parts of applications...and different intentions can achieved structure.
one more thing mention. have changed groupid org.test
org.test.module
in module-parent. useful if have large number of modules cause groupid represents folder structure in repository (as java packages in java project)...this give better overview...
the project-root
location define overall usable dependencies via dependencymanagement etc....and used plugins should defined via pluginmanagement...or may using maven-enforcer-plugin define overall project rules...
typical scenarios kind of structure java ee projects or other large projects (may 300...or 1000 modules yes exist)...
if more modules can use multithread capability of maven , build project with:
mvn -t 4 clean install
from project-root
reduces build time dramatically..
Comments
Post a Comment