Maven可以运行非构建任务吗?

Joe*_*tra 6 java ant maven-3

我从Ant搬到Maven,但想念一件事:执行任意任务的能力。我想摆脱蚂蚁的,build.xml但我仍然只需要它。

有时我需要为XML处理和PDF处理运行一些统计信息。它们不是构建的一部分,但无论如何我都需要使它们自动化。在Ant中,我以前只是在代码中编译并运行Java类以使用javaAnt任务,例如:

<target name="gen-stats">
  <java classname="com.utl.StatsGen" classpath="build" />
</target>

<target name="compute-complexity">
  <java classname="com.utl.PDFComplexity" classpath="lib/pdf-cpx.jar" />
</target>
Run Code Online (Sandbox Code Playgroud)

试图把我的大脑包起来。也许Maven并非旨在提供任何自动化方面的帮助,而只是解决“面向构建”的任务。是吗?

Ari*_*era 5

基本上,Maven定义了阶段目标插件生命周期

阶段:定义的构建生命周期中的阶段。
每个阶段都是一系列目标

目标目标负责一项特定任务

插件插件是一目标目标不一定都绑定到同一阶段)。

生命周期生命周期阶段序列

话虽这么说,但有一组默认的Maven 生命周期

  • 默认值(负责处理项目部署的主要生命周期-构建和部署您的项目-)
  • 清洁(负责处理项目的生命周期)
  • 网站(生命周期负责创建项目的网站文档)

默认生命周期阶段

<phases>
  <phase>validate</phase>
  <phase>initialize</phase>
  <phase>generate-sources</phase>
  <phase>process-sources</phase>
  <phase>generate-resources</phase>
  <phase>process-resources</phase>
  <phase>compile</phase>
  <phase>process-classes</phase>
  <phase>generate-test-sources</phase>
  <phase>process-test-sources</phase>
  <phase>generate-test-resources</phase>
  <phase>process-test-resources</phase>
  <phase>test-compile</phase>
  <phase>process-test-classes</phase>
  <phase>test</phase>
  <phase>prepare-package</phase>
  <phase>package</phase>
  <phase>pre-integration-test</phase>
  <phase>integration-test</phase>
  <phase>post-integration-test</phase>
  <phase>verify</phase>
  <phase>install</phase>
  <phase>deploy</phase>
</phases>
Run Code Online (Sandbox Code Playgroud)

清洁生命周期阶段

<phases>
  <phase>pre-clean</phase>
  <phase>clean</phase>
  <phase>post-clean</phase>
</phases>
<default-phases>
  <clean>
    org.apache.maven.plugins:maven-clean-plugin:2.5:clean
  </clean>
</default-phases>
Run Code Online (Sandbox Code Playgroud)

站点生命周期阶段

<phases>
  <phase>pre-site</phase>
  <phase>site</phase>
  <phase>post-site</phase>
  <phase>site-deploy</phase>
</phases>
<default-phases>
  <site>
    org.apache.maven.plugins:maven-site-plugin:3.3:site
  </site>
  <site-deploy>
    org.apache.maven.plugins:maven-site-plugin:3.3:deploy
  </site-deploy>
</default-phases>
Run Code Online (Sandbox Code Playgroud)

看到您在默认生命周期中没有定义默认阶段。这是因为要在此生命周期中执行的默认阶段是为每个包装(耳,罐,战争,rar,pom等)特定定义的。请参阅默认生命周期绑定

因此,如果您运行“ mvn PHASE”,例如。'mvn install'将执行默认生命周期的INSTALL阶段以及所有先前的阶段(这将执行所有阶段,除了在一个已定义的'install'之后的阶段,'deploy'将不会执行)。

当您运行'mvn PLUGIN:GOAL'时,将执行已定义的插件目标,例如。'MVN编译器:编译'。它将运行所有阶段直至定义的阶段(以及这些阶段中的所有目标),包括您定义的目标。


您可以使用一组插件来执行OS命令或Java进程。也有一个插件可以运行Ant任务,这很有用。

Mojo Exec插件 Mojo exec插件参考

例如。使用命令行执行:

mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
Run Code Online (Sandbox Code Playgroud)

例如。使用POM配置执行:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            ...
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.example.Main</mainClass>
          <arguments>
            <argument>argument1</argument>
            ...
          </arguments>
          <systemProperties>
            <systemProperty>
              <key>myproperty</key>
              <value>myvalue</value>
            </systemProperty>
            ...
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>
Run Code Online (Sandbox Code Playgroud)

Maven Antrun插件 antrun插件参考

例如。POM配置:

<project>
  ...
  <build>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>
Run Code Online (Sandbox Code Playgroud)

maven-antrun-plugin只有一个目标,运行。

例如。命令行执行:

mvn antrun:run
Run Code Online (Sandbox Code Playgroud)

有关用法以及如何在pom.xml中定义Ant目标的更多信息: 用法

Antrun用法示例

1-添加插件定义如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
         <execution>
            <id>my-gen-stats-task</id>
            <phase>pre-site</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <ant antfile="${basedir}/build.xml">
                        <target name="gen-stats"/>
                    </ant>
                </target>
            </configuration>
        </execution>
    </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>
Run Code Online (Sandbox Code Playgroud)

2-执行定义的Maven阶段:

mvn pre-site
Run Code Online (Sandbox Code Playgroud)

您可以使用SITE生命周期进行操作...请尝试以下示例,看看如何仅在“预站点”阶段中定义的ant任务执行时才运行“ mvn预站点”。尝试运行“ MVN站点”,看看如何执行“站点前”和“站点”任务:

例如。

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>my-pre-site</id>
                        <phase>pre-site</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>PRE-SITE PHASE!!!</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>my-site</id>
                        <phase>site</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>SITE PHASE!!!</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>my-post-site</id>
                        <phase>post-site</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>POST-SITE PHASE!!!</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>my-site-deploy</id>
                        <phase>site-deploy</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>SITE DEPLOY PHASE!!!</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)