【问题标题】:tomcat-maven-plugin and pluginManagement with multiple module带有多个模块的 tomcat-maven-plugin 和 pluginManagement
【发布时间】:2014-07-01 09:36:05
【问题描述】:

当我使用 tomcat-plugin 时,我有一个奇怪的行为,在我的父项目中,我在插件管理中声明了插件配置。

我有 3 个子战争项目,其中两个声明了插件,一个没有声明插件。

由于未知原因,插件在第一个项目中执行,但我不明白为什么。

这是我的父项目的示例。

<?xml version="1.0"?>
<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">
  <groupId>my.groupID</groupId>
  <artifactId>parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0.0</version>
  <name>parent</name>

  <build>
    ...
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <server>local</server>
            <update>true</update>
            <charset>UTF-8</charset>                
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

这是 child1 的示例(插件未声明):

<?xml version="1.0"?>
<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>
    <artifactId>parentProject</artifactId>
    <groupId>my.groupID</groupId>
    <version>1.0.0</version>
  </parent>

  <groupId>my.groupID</groupId>
  <artifactId>child1</artifactId>
  <packaging>war</packaging>

  <build>
    ...
  </build>
</project>

这里是child2的示例(插件是声明的)

<?xml version="1.0"?>
<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>
    <artifactId>parentProject</artifactId>
    <groupId>my.groupID</groupId>
    <version>1.0.0</version>
  </parent>

  <groupId>my.groupID</groupId>
  <artifactId>child2</artifactId>
  <packaging>war</packaging>

  <properties>
    <urlTomcat>http://localhost:8080/</urlTomcat>
    <pathApp>child2</pathApp>
  </properties>

  <profiles>
    <profile>
      <id>deploy-child2</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
              <url>${urlTomcat}manager/text</url>
              <path>/${pathApp}</path>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

  <dependencies>
    <dependency>
      <groupId>my.groupID</groupId>
      <artifactId>child1</artifactId>
      <type>war</type>
    </dependency>
  </dependencies>

  <build>
    ...
  </build>
</project>

这是child3的示例(插件是声明的)

    <?xml version="1.0"?>
<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>
    <artifactId>parentProject</artifactId>
    <groupId>my.groupID</groupId>
    <version>1.0.0</version>
  </parent>

  <groupId>my.groupID</groupId>
  <artifactId>child3</artifactId>
  <packaging>war</packaging>

  <properties>
    <urlTomcat>http://localhost:8080/</urlTomcat>
    <pathApp>child3</pathApp>
  </properties>

  <profiles>
    <profile>
      <id>deploy-child3</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
              <url>${urlTomcat}manager/text</url>
              <path>/${pathApp}</path>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

  <dependencies>
    <dependency>
      <groupId>my.groupID</groupId>
      <artifactId>child1</artifactId>
      <type>war</type>
    </dependency>
  </dependencies>

  <build>
    ...
  </build>
</project>

当我运行这个命令时:

mvn tomcat7:deploy -Pdeploy-child2 或 mvn tomcat7:deploy -Pdeploy-child3

我的问题是 maven 尝试部署 child1 项目,我不想要它。我希望 child1 将被构建但不会部署,因为它没有声明插件。

【问题讨论】:

  • 首先,如果您调用mvn tomcat7:deploy,您不会调用生命周期,而是调用插件的目标,在这种情况下称为 maven-tomcat7-plugin 的部署目标。此外,如果您想激活配置文件,您应该使用 mvn -Pdeploy-child2 ...not by mvn -Ddeploy-child2 ... 来执行此操作,这是行不通的。我建议转到您项目的根级别并尝试mvn -Pdeploy-child2 deploy,这将无法正常工作,因为tomcat7-maven-plugin 以deploy 为目标分叉了生命周期。还有一个目标deploy-only可以绑定到生命周期。
  • 好吧,在写这篇文章时,我对 -D 的看法是正确的。我使用 -P 来选择配置文件。我纠正它。
  • 你可以尝试编译到子1战争依赖

标签: java maven tomcat


【解决方案1】:

这个插件即使在 pluginManagement 中提到,也会被子模块继承,即使它不包含在子 pom 中。要了解为什么包含它们,请查看此link。部署目标是战争包的生命周期目标。

在父pom中,包含following,那么它不会被包含在没有定义它的childs中。

    <configuration>
          <skip>true</skip>
   </configuration>

【讨论】:

  • 在这种情况下似乎无法管理跳过。我也尝试在 child1 项目中将其设置为 true,但没有成功。
猜你喜欢
  • 2013-03-31
  • 2015-12-30
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多