├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── maven-multiple-level-modules-sub-parent ├── maven-multiple-level-modules-first │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── zrgs │ │ │ └── maven │ │ │ └── App.java │ │ └── test │ │ └── java │ │ └── org │ │ └── zrgs │ │ └── maven │ │ ├── AppTest.java │ │ └── CopyOfAppTest.java ├── maven-multiple-level-modules-second │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── zrgs │ │ │ └── maven │ │ │ └── App.java │ │ └── test │ │ └── java │ │ └── org │ │ └── zrgs │ │ └── maven │ │ ├── AnotherAppTest.java │ │ └── AnotherCopyOfAppTest.java └── pom.xml ├── mod-pom.xml └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .idea/ 3 | target/ 4 | atlassian-ide-plugin.xml 5 | 6 | *.iml 7 | *.ipr 8 | *.iws 9 | 10 | *.log 11 | .classpath 12 | 13 | effective.pom 14 | pom.xml.releaseBackup 15 | release.properties 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | addons: 3 | apt: 4 | packages: 5 | - oracle-java8-installer 6 | 7 | language: java 8 | 9 | # env: 10 | # - 11 | jdk: 12 | - oraclejdk8 13 | 14 | script: mvn clean verify -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Andy Glick 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/andyglick/maven-multi-level-modules-project.png)](https://travis-ci.org/andyglick/maven-multi-level-modules-project) 2 | # Invoking builds into Multiple Level Maven Module Structures 3 | 4 | My friend Rob recently asked me if I knew how to execute a single test in a Maven module that was nested 2 or more levels below the parent pom and I 5 | didn't know how to do that, and none of the suggestions that I made worked for him in his environment. I didn't have any visibility into his project's 6 | directory structure and sending emails back and forth wasn't really getting us anywhere very quickly. Finally I bit the bullet and put together my own little 7 | example Maven project. In hopes that others might learn something useful from it, I published it. 8 | 9 | This is the directory structure of the project 10 | 11 | maven-multiple-level-modules-parent 12 | maven-multiple-level-modules-sub-parent 13 | maven-multiple-level-modules-first 14 | maven-multiple-level-modules-second 15 | 16 | There is a pom in each module directory. In the 3 modules below the root module, each references the one above it as it's parent. The 2 parent modules each 17 | reference their respective children in its own *modules* section. 18 | 19 | ## The parent pom 20 | 21 | There is a parent pom in the top level module, whose type is pom 22 | 23 | ## The sub-parent pom 24 | 25 | Directly below the parent module there is another module with a parent pom. Let's refer to it as the sub-parent module containing a sub-parent pom, and its 26 | type is also pom 27 | 28 | ## The pom children 29 | 30 | Layered under the sub-parent module are 2 child modules whose poms each have jar as their type. 31 | 32 | ## The Problem 33 | 34 | It may be worth saying that when I looked at this project this evening I no longer remembered what the Maven **-pl** option 35 | indicated, it turns out that **-pl** is an abreviation for **--projects**. **--projects** can ne strung out one after the other 36 | with commas used as delimiters 37 | 38 | So if the goal was to execute from the project's base dir the test phase of only 1 of the pom children, and beyond that to 39 | fire only 1 test class from that module, or to be even more 40 | discriminating, to execute only 1 test from the chosen test class, then at a minimum the **-pl** option would have to be used to name the module. But what I 41 | couldn't figure out was how to specify the name of the module correctly so that the Maven engine would recognize it and run the surefire plugin on it. In the 42 | end, and what I often find to be the case when munging around with Maven internals, **-X** is your friend. What I saw in the Maven run log was: 43 | 44 | [INFO] Scanning for projects... 45 | [INFO] ------------------------------------------------------------------------ 46 | [INFO] Reactor Build Order: 47 | [INFO] 48 | [INFO] maven-multiple-level-modules-parent 49 | [INFO] maven-multiple-level-modules-sub-parent 50 | [INFO] maven-multiple-level-modules-first 51 | [INFO] maven-multiple-level-modules-second 52 | [INFO] 53 | [INFO] ------------------------------------------------------------------------ 54 | [INFO] Building maven-multiple-level-modules-parent 1. 55 | [INFO] ------------------------------------------------------------------------ 56 | [INFO] 57 | [INFO] ------------------------------------------------------------------------ 58 | [INFO] Building maven-multiple-level-modules-sub-parent 1.0 59 | [INFO] ------------------------------------------------------------------------ 60 | [INFO] 61 | [INFO] ------------------------------------------------------------------------ 62 | [INFO] Building maven-multiple-level-modules-first 1.0 63 | [INFO] ------------------------------------------------------------------------ 64 | [DEBUG] === REACTOR BUILD PLAN ================================================ 65 | [DEBUG] Project: org.zrgs.maven:maven-multiple-level-modules-parent:pom:1.0 66 | [DEBUG] Tasks: [test] 67 | [DEBUG] Style: Regular 68 | [DEBUG] ----------------------------------------------------------------------- 69 | [DEBUG] Project: org.zrgs.maven:maven-multiple-level-modules-sub-parent:pom:1.0 70 | [DEBUG] Tasks: [test] 71 | [DEBUG] Style: Regular 72 | [DEBUG] ----------------------------------------------------------------------- 73 | [DEBUG] Project: org.zrgs.maven:maven-multiple-level-modules-first:jar:1.0 74 | [DEBUG] Tasks: [test] 75 | [DEBUG] Style: Regular 76 | [DEBUG] ----------------------------------------------------------------------- 77 | [DEBUG] Project: org.zrgs.maven:maven-multiple-level-modules-second:jar:1.0 78 | [DEBUG] Tasks: [test] 79 | [DEBUG] Style: Regular 80 | [DEBUG] ======================================================================= 81 | 82 | So it turns out that the GAV for each module applies here. GAV is a Maven technical concept that resolves to GroupId, ArtifactId and Version. And what you 83 | can see on each of the "[DEBUG] Project:" lines is in fact the GAV of the module to be executed. 84 | 85 | Why this important piece of information is hidden in the DEBUG output and isn't included in the default INFO text is beyond me. It is another "interesting" 86 | design decision on the part of the Maven developers. 87 | 88 | ## The Solution 89 | 90 | So in the case of executing the test phase only in the maven-multiple-level-modules-first module the syntax turned 91 | out to be: 92 | 93 | mvn test -pl org.zrgs.maven:maven-multiple-level-modules-first 94 | 95 | To execute only the AppTest test class during that test phase the syntax is: 96 | 97 | mvn test -pl org.zrgs.maven:maven-multiple-level-modules-first -Dtest=AppTest 98 | 99 | And to execute only the testOfCopyOfTestApp in the TestApp class the syntax is: 100 | 101 | mvn test -pl org.zrgs.maven:maven-multiple-level-modules-first -Dtest=AppTest#testOfCopyOfTestApp 102 | 103 | For completists, I was able to craft the syntax to explicitly execute multiple tests from a single test class 104 | using the "+" as the between test delimiter. Surefire is now capable of handling that, it no longer throws exceptions 105 | 106 | The pom file uses maven-surefire-plugin:3.0.0-M7 and junit:4.13.2 107 | 108 | mvn test -pl org.zrgs.maven:maven-multiple-level-modules-first -Dtest=AppTest#testOfCopyOfTestApp+testApp 109 | 110 | I removed all of the references to the late and not especially lamented versioneye service from any and all 111 | pom files 112 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-first/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | org.zrgs.maven 7 | maven-multiple-level-modules-sub-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | 11 | maven-multiple-level-modules-first 12 | maven-multiple-level-modules-first 13 | http://maven.apache.org 14 | 15 | 16 | 17 | junit 18 | junit 19 | test 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-first/src/main/java/org/zrgs/maven/App.java: -------------------------------------------------------------------------------- 1 | package org.zrgs.maven; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-first/src/test/java/org/zrgs/maven/AppTest.java: -------------------------------------------------------------------------------- 1 | package org.zrgs.maven; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertTrue; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Not a Rigourous Test :-) 14 | */ 15 | @Test 16 | public void testApp() 17 | { 18 | assertTrue( true ); 19 | } 20 | 21 | @Test 22 | public void testOfCopyOfTestApp() 23 | { 24 | assertTrue( true ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-first/src/test/java/org/zrgs/maven/CopyOfAppTest.java: -------------------------------------------------------------------------------- 1 | package org.zrgs.maven; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertTrue; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class CopyOfAppTest 11 | { 12 | /** 13 | * Not a Rigourous Test :-) 14 | */ 15 | @Test 16 | public void testApp() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-second/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | org.zrgs.maven 7 | maven-multiple-level-modules-sub-parent 8 | 1.0.4-SNAPSHOT 9 | 10 | 11 | maven-multiple-level-modules-second 12 | 13 | maven-multiple-level-modules-second 14 | http://maven.apache.org 15 | 16 | 17 | 18 | junit 19 | junit 20 | test 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-second/src/main/java/org/zrgs/maven/App.java: -------------------------------------------------------------------------------- 1 | package org.zrgs.maven; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-second/src/test/java/org/zrgs/maven/AnotherAppTest.java: -------------------------------------------------------------------------------- 1 | package org.zrgs.maven; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertTrue; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AnotherAppTest 11 | { 12 | /** 13 | * 14 | * Not a Rigourous Test :-) 15 | */ 16 | @Test 17 | public void testApp() 18 | { 19 | assertTrue( true ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/maven-multiple-level-modules-second/src/test/java/org/zrgs/maven/AnotherCopyOfAppTest.java: -------------------------------------------------------------------------------- 1 | package org.zrgs.maven; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertTrue; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AnotherCopyOfAppTest 11 | { 12 | /** 13 | * Not a Rigourous Test :-) 14 | */ 15 | @Test 16 | public void testApp() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /maven-multiple-level-modules-sub-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | maven-multiple-level-modules-parent 7 | org.zrgs.maven 8 | 1.0.4-SNAPSHOT 9 | 10 | 11 | maven-multiple-level-modules-sub-parent 12 | 13 | pom 14 | maven-multiple-level-modules-sub-parent 15 | 16 | 17 | maven-multiple-level-modules-first 18 | maven-multiple-level-modules-second 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /mod-pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.zrgs.maven 7 | maven-multiple-level-modules-parent 8 | 1.0.4-SNAPSHOT 9 | pom 10 | maven-multiple-level-modules-parent 11 | 12 | 13 | 14 | internal.repo 15 | Temporary Staging Repository 16 | file://${project.build.directory}/mvn-repo 17 | 18 | 19 | 20 | 21 | scm:git:https://github.com/andyglick/maven-multi-level-modules-project.git 22 | HEAD 23 | 24 | 25 | 26 | 27 | jenkins.ci 28 | Jenkins Maven Repo 29 | http://repo.jenkins-ci.org/releases 30 | 31 | true 32 | 33 | 34 | true 35 | always 36 | 37 | 38 | 39 | 40 | 41 | 42 | jenkins.ci 43 | Jenkins Maven Repo 44 | http://repo.jenkins-ci.org/releases 45 | 46 | true 47 | 48 | 49 | true 50 | 51 | 52 | 53 | 54 | 55 | 4.0.0-beta-1 56 | 3.7.1 57 | 3.5.0 58 | 3.4.1 59 | 1.8 60 | ${java.version} 61 | ${java.version} 62 | 4.0.0-beta-3 63 | 3.6.3 64 | UTF-8 65 | 4.0.0-beta-1plain 66 | false 67 | 3.3.1 68 | 2.17.0 69 | 70 | 71 | 72 | maven-multiple-level-modules-sub-parent 73 | 74 | 75 | 76 | 77 | 78 | junit 79 | junit 80 | 4.13.2 81 | 82 | 83 | 84 | 85 | 86 | 87 | coverage-generate 88 | 89 | 90 | 91 | org.jacoco 92 | jacoco-maven-plugin 93 | 0.7.9 94 | 95 | 96 | prepare-agent 97 | 98 | prepare-agent 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | coverage-report 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | org.apache.maven.plugins 125 | maven-resources-plugin 126 | ${resources.plugin.version} 127 | 128 | 129 | 130 | maven-compiler-plugin 131 | ${compiler.plugin.version} 132 | 133 | 134 | maven-dependency-plugin 135 | ${dependency.plugin.version} 136 | 137 | 138 | maven-failsafe-plugin 139 | ${surefire.version} 140 | 141 | 142 | maven-help-plugin 143 | ${help.plugin.version} 144 | 145 | 146 | maven-surefire-plugin 147 | ${surefire.version} 148 | 149 | 150 | org.codehaus.mojo 151 | versions-maven-plugin 152 | ${versions.plugin.version} 153 | 154 | 155 | 156 | 157 | 158 | org.apache.maven.plugins 159 | maven-enforcer-plugin 160 | ${enforcer.plugin.version} 161 | 162 | 163 | enforce-dependency-convergence 164 | validate 165 | 166 | enforce 167 | 168 | 169 | 170 | 171 | 172 | 173 | 1.8 174 | 175 | 176 | [${minimum.maven.version},${maven.version}] 177 | 178 | 179 | 180 | 181 | commons-logging 182 | log4j 183 | log4j:log4j 184 | 185 | jdom:jdom 186 | 187 | org.apache.commons:commons-io 188 | 189 | true 190 | 191 | 192 | 193 | 194 | 195 | 196 | maven-release-plugin 197 | 2.5.3 198 | 199 | 200 | maven-dependency-plugin 201 | 202 | 203 | maven-help-plugin 204 | 205 | 206 | org.codehaus.mojo 207 | versions-maven-plugin 208 | 209 | 210 | 211 | 212 | 213 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.zrgs.maven 7 | maven-multiple-level-modules-parent 8 | 1.0.4-SNAPSHOT 9 | pom 10 | maven-multiple-level-modules-parent 11 | 12 | 13 | 14 | internal.repo 15 | Temporary Staging Repository 16 | file://${project.build.directory}/mvn-repo 17 | 18 | 19 | 20 | 21 | scm:git:https://github.com/andyglick/maven-multi-level-modules-project.git 22 | HEAD 23 | 24 | 25 | 26 | 27 | jenkins.ci 28 | Jenkins Maven Repo 29 | http://repo.jenkins-ci.org/releases 30 | 31 | true 32 | 33 | 34 | true 35 | always 36 | 37 | 38 | 39 | 40 | 41 | 42 | jenkins.ci 43 | Jenkins Maven Repo 44 | http://repo.jenkins-ci.org/releases 45 | 46 | true 47 | 48 | 49 | true 50 | 51 | 52 | 53 | 54 | 55 | 3.10.1 56 | 3.3.0 57 | 3.1.0 58 | 3.3.0 59 | 1.8 60 | ${java.version} 61 | ${java.version} 62 | 3.8.6 63 | 3.6.3 64 | UTF-8 65 | plain 66 | false 67 | 3.0.0-M7 68 | 2.12.0 69 | 70 | 71 | 72 | maven-multiple-level-modules-sub-parent 73 | 74 | 75 | 76 | 77 | 78 | junit 79 | junit 80 | 4.13.2 81 | 82 | 83 | 84 | 85 | 86 | 87 | coverage-generate 88 | 89 | 90 | 91 | org.jacoco 92 | jacoco-maven-plugin 93 | 0.7.9 94 | 95 | 96 | prepare-agent 97 | 98 | prepare-agent 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | coverage-report 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | maven-compiler-plugin 125 | ${compiler.plugin.version} 126 | 127 | 128 | maven-dependency-plugin 129 | ${dependency.plugin.version} 130 | 131 | 132 | maven-failsafe-plugin 133 | ${surefire.version} 134 | 135 | 136 | maven-help-plugin 137 | ${help.plugin.version} 138 | 139 | 140 | maven-surefire-plugin 141 | ${surefire.version} 142 | 143 | 144 | org.codehaus.mojo 145 | versions-maven-plugin 146 | ${versions.plugin.version} 147 | 148 | 149 | 150 | 151 | 152 | org.apache.maven.plugins 153 | maven-enforcer-plugin 154 | ${enforcer.plugin.version} 155 | 156 | 157 | enforce-dependency-convergence 158 | validate 159 | 160 | enforce 161 | 162 | 163 | 164 | 165 | 166 | 167 | 1.8 168 | 169 | 170 | [${minimum.maven.version},${maven.version}] 171 | 172 | 173 | 174 | 175 | commons-logging 176 | log4j 177 | log4j:log4j 178 | 179 | jdom:jdom 180 | 181 | org.apache.commons:commons-io 182 | 183 | true 184 | 185 | 186 | 187 | 188 | 189 | 190 | maven-release-plugin 191 | 2.5.3 192 | 193 | 194 | maven-dependency-plugin 195 | 196 | 197 | maven-help-plugin 198 | 199 | 200 | org.codehaus.mojo 201 | versions-maven-plugin 202 | 203 | 204 | 205 | 206 | 207 | --------------------------------------------------------------------------------