1. Introduction
1.介绍
Maven and Ant are both well-known build automation tools for Java. Although most of the time we’ll only use one of these, there are cases when using the two together makes sense.
Maven和Ant都是著名的Java构建自动化工具。虽然大多数时候我们只使用其中之一,但在某些情况下,将这两个工具放在一起使用是有意义的。
A common use case is when working on a legacy project that uses Ant, and we want to introduce Maven gradually while still keeping some existing Ant tasks in place.
一个常见的用例是,当我们在一个使用Ant的传统项目上工作时,我们想逐步引入Maven,同时仍保留一些现有的Ant任务。
In this tutorial, we’ll cover how to do this using the Maven AntRun Plugin.
在本教程中,我们将介绍如何使用Maven AntRun插件实现这一目标。
2. Maven AntRun Plugin
2.MavenAntRun插件
Maven AntRun Plugin allows us to run Ant tasks within Maven.
Maven AntRun插件使我们能够在Maven中运行Ant任务。
2.1. Adding the Plugin
2.1.添加插件
To use this plugin, we need to add it to our Maven project’s build plugins:
要使用这个插件,我们需要将其添加到Maven项目的构建插件中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
...
</executions>
</plugin>
The latest plugin version can be found on the Maven Central (although it hasn’t been updated in a long time).
最新的插件版本可以在Maven Central上找到(尽管它已经很久没有更新了)。
2.2. Plugin Executions
2.2.插件的执行
As with any other Maven plugin, to make use of AntRun plugin, we need to define executions.
与其他Maven插件一样,要使用AntRun插件,我们需要定义执行。
In the example below, we’re defining one execution tied to Maven’s package phase, which will zip the final JAR file from the project’s target directory:
在下面的例子中,我们定义了一个与Maven的package阶段绑定的执行,它将从项目的目标目录中压缩最终的JAR文件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ant-run-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>zip-artifacts</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<zip destfile="${project.basedir}/package.zip"
basedir="${project.build.directory}"
includes="*.jar" />
</target>
</configuration>
</execution>
</executions>
</plugin>
To execute the plugin, we run the command:
为了执行该插件,我们运行该命令。
mvn package
Since we declared our plugin to run during Maven’s package phase, running Maven’s package goal will execute our plugin configuration above.
由于我们声明我们的插件在Maven的package阶段运行,所以运行Maven的package目标将执行我们上面的插件配置。
3. Example Using build.xml File
3.使用build.xml文件的例子
Aside from allowing us to define Ant targets in plugin configuration, we can also use an existing Ant build.xml file.
除了允许我们在插件配置中定义Ant目标外,我们还可以使用现有的Ant build.xml 文件。
3.1. build.xml
3.1.build.xml
Below is an example of a project’s Ant build.xml file with a target defined to upload zip files from the project’s base directory to an FTP server:
下面是一个项目的Ant build.xml文件的例子,其中定义了一个目标,即从项目的基本目录上传zip文件到FTP服务器。
<project name="MyProject" default="dist" basedir=".">
<description>Project Description</description>
...
<target name="ftpArtifact">
<ftp
server="${ftp.host}"
userid="${ftp.user}"
password="${ftp.password}">
<fileset dir="${project.basedir}>
<include name="**/*.zip" />
</fileset>
</ftp>
</target>
</project>
3.2. Plugin Configuration
3.2.插件配置
To use the above build.xml file, we define the execution in our plugin declaration:
为了使用上述build.xml文件,我们在插件声明中定义执行。
<execution>
<id>deploy-artifact</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="ftpArtifact"/>
</ant>
</target>
</configuration>
</execution>
Since the ftp task isn’t included in ant.jar, we need to add Ant’s optional dependencies to our plugin configuration:
由于ftp任务不包括在ant.jar中,我们需要在我们的插件配置中添加Ant的可选依赖项。
<plugin>
<executions>
...
</executions>
<dependencies>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-commons-net</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
</plugin>
To execute the plugin, we run the command:
为了执行该插件,我们运行该命令。
mvn install
4. Conclusion
4.结论
In this short article, we’ve discussed running Ant tasks with Maven’s AntRun plugin. Even though it’s a very simple plugin, having only one goal, this plugin can prove to be effective in projects and teams that prefer the use of Ant for specific build instructions.
在这篇短文中,我们讨论了用Maven的AntRun插件运行Ant任务。尽管这是一个非常简单的插件,只有一个目标,但对于那些喜欢使用Ant进行特定构建指令的项目和团队来说,这个插件可以证明是有效的。
And, if you want to learn more about Ant and Maven, you can read our article, comparing these two – along with Gradle.
如果您想了解更多关于Ant和Maven的信息,您可以阅读我们的文章,对这两者以及Gradle进行比较。