防止在多模块Maven中找到“未找到插件”

在多模块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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开发框架,用于简化基于Java的应用程序的开发和部署。它提供了一个自动化配置的方式来简化开发过程,同时也提供了一些常用的功能模块,如安全、数据库连接等。 ProGuard是一个代码混淆工具,用于保护Java代码免受逆向工程和代码分析的攻击。它通过对代码进行优化和混淆,使得反编译后的代码难以理解和修改,从而增加了代码的安全性。 Maven是一个项目管理工具,用于构建、发布和管理Java项目的依赖关系。它可以帮助我们方便地管理项目的依赖关系,包括第三方库和插件。同时,它还提供了一些命令和配置,来支持多模块的项目结构。 在使用Spring Boot、ProGuard和Maven实现代码混淆时,我们可以按照以下步骤进行操作: 1. 在Maven创建一个多模块的项目结构。通过使用Maven的父子项目关系,我们可以在一个主项目管理多个子模块。 2. 在子模块引入Spring Boot和ProGuard的依赖。在子模块的pom.xml文件,添加对Spring Boot和ProGuard的相关依赖配置。 3. 配置ProGuard的混淆规则。在子模块创建一个proguard.cfg文件,并添加相关的混淆规则。这些规则可以用于指定哪些类、方法和字段需要进行混淆,以及如何进行混淆。 4. 在Maven的构建过程,添加对ProGuard的插件配置。通过对Maven插件进行配置,使得在构建项目时自动应用ProGuard的混淆规则。 5. 构建和执行项目。在Maven使用命令进行项目的构建和执行,观察代码是否已经被混淆。如果一切顺利,你将会得到一个经过混淆的代码。 综上所述,通过使用Spring Boot、ProGuard和Maven,我们可以实现对Java代码的混淆保护。这种方式可以增加代码的安全性,防止代码被逆向工程和代码分析的攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值