项目结构类似:

petstore-parent
|--petstore-api
|--petstore-server
|--petstore-common
|--petstore-bootstrap

其他项目只依赖petstore-api这个模块,所以,只需要deploy这个模块到远程仓库就可以了。 但是petstore-api依赖petstore-parent(在父模块的pom文件中统一管理依赖和相关配置,petstore-parent的package方式为pom) 所以,需要把petstore-api和petstore-parent deploy到远程仓库。

如果在petstore-parent目录下执行mvn deploy, 会把所有的四个子模块都会deploy到远程仓库,这很明显不是我们想要的。

执行mvn deploy命令时,实际执行了maven-deploy-plugin插件,而这个插件有个配置可以设置跳过部署该模块。参考(https://maven.apache.org/plugins/maven-deploy-plugin/faq.html#skip)

<build>
    <!-- ... -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

而针对我们上面的那个例子,为了避免在各个子模块中都增加这段配置,可以统一在父模块的POM文件中增加如下配置。

<project> 
  ...
  <properties>
    <maven.deploy.skip>false</maven.deploy.skip>
  </properties>
  ...
  <build>
      <pluginManagement>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-deploy-plugin</artifactId>
                  <configuration>
                      <skip>${maven.deploy.skip}</skip>
                  </configuration>
              </plugin>
          </plugins>
      </pluginManagement>
  </build>
  ...
</project>

然后在不需要deploy的子模块(此例子中为petstore-server, petstore-common, petstore-bootstrap)的POM文件中增加如下配置:

<properties>
    ...
    <maven.deploy.skip>true</maven.deploy.skip>
</properties>

这样在父模块的目录下执行mvn deploy时就只会deploy petstore-api和petstore-parent了。

更多参考资料(https://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html#skip)。值得注意的是,这个配置是Maven 2.4版本添加的,也就是2.4+的版本才支持此配置。

如果一个父项目下的子项目大部分都不需要deploy,就像我们开头提的那种场景,可以通过maven的-N(-N,–non-recursive Do not recurse into sub-projects)选项来先deploy parent pom,然后切到子项目下deploy子项目。 比如:

//in petstore-parent directory
mvn -N deploy
cd petstore-api
mvn deploy