从Maven运行特定的独立ant目标

时间:2014-11-05 16:43:37

标签: java maven maven-antrun-plugin

我在我的POM中定义了以下插件,试图在我们的测试或生产tomcat服务器上部署maven生成的.war文件:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>
  <configuration>
    <target name="test-deploy">
      <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
        <deploy 
          path="/${project.name}" 
          url="http://test-server:8080/manager/text" 
          username="user" 
          password="pass"
          war="file:${project.build.directory}/${project.build.finalName}.${project.packaging}" 
          update="true"/>
    </target>
    <target name="prod-deploy">
      <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
        <deploy 
          path="/${project.name}" 
          url="http://prod-server:8080/manager/text" 
          username="user" 
          password="pass"
          war="file:${project.build.directory}/${project.build.finalName}.${project.packaging}" 
          update="true"/>
    </target>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-catalina-ant</artifactId>
      <version>8.0.14</version>
    </dependency>
  </dependencies>
</plugin>

从阅读Maven AntRun Plugin usage看起来我应该能够添加目标名称(我已经完成),但我不确定如何调用特定目标。即使我尝试仅调用web-deploy目标,test-deploy目标也是唯一的目标:

mvn antrun:run -Dtarget=test-deploy

如何指定我想要运行的目标?

2 个答案:

答案 0 :(得分:1)

AFAIK,这是不可能的。

我建议使用maven profiles拆分maven任务并使用maven根据条件或命令行参数触发配置文件的能力调用它们。
这意味着每个配置文件都会声明自己的 maven-antrun-plugin ,但会让您灵活地单独调用它们。

我希望这会有所帮助。

答案 1 :(得分:0)

根据Eldad AK的回答,这是一个简单的工作解决方案,使用maven配置文件来自定义该配置文件的ant目标运行:

        ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.7</version>
          <configuration>
            <target>
              <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
                <echo message="Deploying to ${tomcat.deploy.url}"/>
                <deploy 
                  path="/${project.name}" 
                  url="${tomcat.deploy.url}" 
                  username="${tomcat.deploy.username}" 
                  password="${tomcat.deploy.password}"
                  war="file:${project.build.directory}/${project.build.finalName}.${project.packaging}" 
                  update="true"/>
            </target>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>org.apache.tomcat</groupId>
              <artifactId>tomcat-catalina-ant</artifactId>
              <version>8.0.14</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </build>

    <profiles>
      <profile>
        <id>test</id>
        <properties>
          <tomcat.deploy.url>http://test-server:8080/manager/text</tomcat.deploy.url>
          <tomcat.deploy.username>user</tomcat.deploy.username>
          <tomcat.deploy.password>pass</tomcat.deploy.password>
        </properties>
      </profile>
      <profile>
        <id>prod</id>
        <properties>
          <tomcat.deploy.url>http://prod-server:8080/manager/text</tomcat.deploy.url>
          <tomcat.deploy.username>user</tomcat.deploy.username>
          <tomcat.deploy.password>pass</tomcat.deploy.password>
        </properties>
      </profile>
    </profiles>

要部署到测试或产品,只需选择适当的配置文件:

> mvn -Pprod antrun:run

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ExampleApplication 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.7:run (default-cli) @ ExampleApplication ---
[INFO] Executing tasks

main:
     [echo] Deploying to http://prod-server:8080/manager/text
   [deploy] OK - Deployed application at context path /ExampleApplication
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------