Brice Roncace
Brice Roncace

Reputation: 10650

Run specific standalone ant target from Maven

I've got the following plugin defined in my POM in an attempt to deploy the maven produced .war file either on our test or production tomcat server:

<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>

From reading the Maven AntRun Plugin usage it looks like I should be able to add a target name (as I've done), but I'm not sure how to invoke a specific target. The web-deploy target is the only target run even if I try to invoke only the test-deploy target with:

mvn antrun:run -Dtarget=test-deploy

How do I specify which ant target I'd like to run?

Upvotes: 1

Views: 1058

Answers (2)

Brice Roncace
Brice Roncace

Reputation: 10650

Based on Eldad AK's answer, here is a simple working solution using maven profiles to customize the ant target run for that profile:

        ...
        <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>

To deploy to test or prod, just choose the appropriate profile:

> 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] ------------------------------------------------------------------------

Upvotes: 0

Eldad Assis
Eldad Assis

Reputation: 11045

AFAIK, this is not possible.

I suggest using maven profiles to split the maven tasks and calling them with maven's ability to trigger profiles based on conditions or command line parameters.
It means that each profile will declare its own maven-antrun-plugin, but will give you the flexibility to call them individually.

I hope this helps.

Upvotes: 1

Related Questions