2. maven调用ant脚本

作者: OkGogogooo | 来源:发表于2022-04-26 10:26 被阅读0次

    1. 环境说明

    eclipse 2021-12R
    maven 3.8.5

    2 . 目标

    在进行deploy的时候,调用一个在工程里面已经存在的ant脚本。ant脚本里面使用了ant-contrib-1.0b3.jar

    3. 脚本

    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      ... 省略
      <build>
            <sourceDirectory>src/main/java</sourceDirectory>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
            </resources>
            <testSourceDirectory>src/test/java</testSourceDirectory>
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                </testResource>
            </testResources>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <!--
                        <release>1.8</release>
                        -->
                        <source>1.8</source>
                        <target>1.8</target>
                        <compilerVersion>1.8</compilerVersion>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.2.0</version>
                    <configuration>
                        <archive>
                            <manifestFile>META-INF/MANIFEST.MF</manifestFile>
                            <manifest>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            </manifest>
                            <manifestEntries>
                                <Build-Time>${maven.build.timestamp}</Build-Time>
                                <XZ-Pub-Time>${maven.build.timestamp}</XZ-Pub-Time>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>3.1.0</version>
                    <executions>
                        <execution>
                            <id>pack-eclipse-plugin</id>
                            <phase>deploy</phase>
                            <configuration>
                                <target name="pack_eclipse_plugin">
                                    <echo>开始调用Ant脚本...</echo>
                                    <!-- publish.ant和pom.xml是同一个目录下-->
                                    <ant antfile="publish.ant" target="pack" />
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.ant</groupId>
                            <artifactId>ant</artifactId>
                            <version>1.10.12</version>
                        </dependency>
                        <dependency>
                            <groupId>ant-contrib</groupId>
                            <artifactId>ant-contrib</artifactId>
                            <version>1.0b3</version>
                         </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
        
        <distributionManagement>
            <repository>
                    ... 省略
            </repository>
        </distributionManagement>
    </project>
    

    4. 出现的问题

    ... 省略
    Caused by: java.lang.NoSuchMethodError: org.apache.tools.ant.util.FileUtils.getFileUtils()Lorg/apache/tools/ant/util/FileUtils;
       at org.apache.tools.ant.taskdefs.optional.ReplaceRegExp.<clinit> (ReplaceRegExp.java:127)
       at java.lang.Class.forName0 (Native Method)
       at java.lang.Class.forName (Class.java:264)
       at org.apache.tools.ant.Project.init (Project.java:269)
       at org.apache.maven.plugins.antrun.AntRunMojo.execute (AntRunMojo.java:285)
    ... 省略
    

    原因:ant-contrib这个dependency依赖的ant是1.5及以上,如果不通过

    <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant</artifactId>
        <version>1.10.12</version>
    </dependency>
    

    这段声明ant的版本,会将ant1.5拉取下来,造成找不到方法。

    相关文章

      网友评论

        本文标题:2. maven调用ant脚本

        本文链接:https://www.haomeiwen.com/subject/jwdcyrtx.html