在多模块Maven项目的子模块上定义Maven插件会给我们“找不到插件”错误。 尤其是如果我们有一个多模块项目,并且只想在一个特定模块中应用Maven插件,则此错误会经常发生。
假设我们有一个看起来像这样的多模块root 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>com.jdriven.blog</groupId>
<artifactId>maven-plugin-multimodule</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module> <!-- Module1 is a regular jar -->
<module>module2</module> <!-- Module2 has tomcat7 plugin configured -->
</modules>
</project>
本能地将插件(例如tomcat7)添加到此特定模块module2
,就像这样。
<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>maven-plugin-multimodule-module2</artifactId>
<packaging>war</packaging>
<parent>
<groupId>com.jdriven.blog</groupId>
<artifactId>maven-plugin-multimodule</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- This is where our specific configuration goes -->
</configuration>
</plugin>
</plugins>
</build>
</project>
在多模块root pom上运行命令mvn tomcat7:help
时,出现以下错误:
[ERROR] No plugin found for prefix 'tomcat7' in the current project
and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo]
available from the repositories
解决方案非常简单:我们在多模块root pom的pluginManagement部分中指定插件。
<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.jdriven.blog</groupId>
<artifactId>maven-plugin-multimodule</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module> <!-- Module1 is a regular jar -->
<module>module2</module> <!-- Module2 has tomcat7 plugin configured -->
</modules>
<build>
<!-- In the multi-module root pom, use the pluginManagement to define the version of the maven-plugin -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
并且在我们特定的模块module2
我们清除了插件的版本,因为它已在多模块root 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>maven-plugin-multimodule-module2</artifactId>
<packaging>war</packaging>
<parent>
<groupId>com.jdriven.blog</groupId>
<artifactId>maven-plugin-multimodule</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<!-- No version needed here, it is already defined in the multi-module root pom -->
<configuration>
<!-- This is where our specific configuration goes -->
</configuration>
</plugin>
</plugins>
</build>
</project>
现在,何时可以运行命令mvn tomcat7:help
而不会出现任何错误。 我们准备在子模块上配置插件。
翻译自: https://www.javacodegeeks.com/2015/03/prevent-no-plugin-found-in-multi-module-maven.html