maven:調用ant執行build.xml的兩種方式

來源:互聯網
上載者:User

sql2java是通過ant指令碼組織項目來實現java代碼產生的,所以要使用sql2java就必須使用ant來執行build.xml指令碼。如果自己項目是用maven來組織管理的,那麼要使用sql2java來產生代碼就涉及到maven執行ant指令碼的問題。
maven提供了執行ant指令碼的功能,這裡就用到了maven外掛程式maven-antrun-plugin。

sql2java/pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><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 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>    <groupId>sql2java</groupId>  <artifactId>sql2java-generator</artifactId>    <packaging>pom</packaging>  <version>1.0-SNAPSHOT</version>  <name>sql2java-generator</name>    <!-- 調用 ant 執行 build.xml 完成jav代碼產生  -->  <properties>      <!--        sql2java參數定義,參見build.xml      調用方式      mvn  generate-sources \            -Dsql2java-config=../sql2java.properties \            -Dgenerated-src=../src/main/java \            -Ddriver-jar=../lib/mysql-connector-java-5.1.43-bin.jar      -->    <sql2java-config>../sql2java.properties</sql2java-config>    <generated-src>../src/main/java</generated-src>    <!--<driver-jar>../lib/mysql-connector-java-5.1.43-bin.jar</driver-jar>-->  </properties>    <build>        <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-antrun-plugin</artifactId>                <version>1.8</version>                <executions>                    <execution>                        <id>ant-build</id>                        <!--綁定外掛程式到代碼產生階段執行-->                        <phase>generate-sources</phase>                        <goals>                        <goal>run</goal>                        </goals>                        <configuration>                        <target>                            <!--                            執行builx.xml指令碼,                            並向build.xml傳遞三個參數 sql2java-config,generated-src,driver-jar                            -->                            <ant antfile="build.xml" target="rebuild" inheritAll="false">                                <property name="sql2java-config" value="${sql2java-config}"/>                                <property name="generated-src" value="${generated-src}"/>                                <property name="driver-jar" value="${driver-jar}"/>                            </ant>                        </target>                        </configuration>                    </execution>            </executions>        </plugin>        </plugins>    </build></project>
指定生命週期階段(phase)

上面的指令碼中將antrun外掛程式綁定在generate-sources 生命週期(phase),並給antrun指定了要執行的具體任務(<target></target>定義),那麼maven執行時,只要指定生命週期(phase)為generate-sources,就可以執行buid.xml指令碼了,如下:

mvn  generate-sources \    -Dsql2java-config=../sql2java.properties \    -Dgenerated-src=../src/main/java \    -Ddriver-jar=../lib/mysql-connector-java-5.1.43-bin.jar
指定外掛程式目標(goal)

sql2java/pom.xml中antrun外掛程式也可以如下定義,即不將它綁定到任何生命週期(phase),而是通過在命令列指定執行外掛程式目標(goal)的方式來執行build.xml指令碼:

<plugin>    <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-antrun-plugin</artifactId>        <version>1.8</version>              <configuration>            <target>                <!--                執行builx.xml指令碼,                並向build.xml傳遞三個參數 sql2java-config,generated-src,driver-jar                -->                <ant antfile="build.xml" target="rebuild" inheritAll="false">                    <property name="sql2java-config" value="${sql2java-config}"/>                    <property name="generated-src" value="${generated-src}"/>                    <property name="driver-jar" value="${driver-jar}"/>                </ant>            </target>        </configuration></plugin>

上面的代碼與本文最開始的sql2java/pom.xml中<plugin></plugin>定義相比,就是少了<execution><executions></execution></executions>兩層,只將configuration元素提到最上層。
這樣以來,意思就變了,maven調用的方式也要做相應的修改,就要在命令列指定外掛程式目標(goal),而不是生命週期階段(phase)

mvn  antrun:run \    -Dsql2java-config=../sql2java.properties \    -Dgenerated-src=../src/main/java \    -Ddriver-jar=../lib/mysql-connector-java-5.1.43-bin.jar
參考資料

build.xml完整代碼參見:https://github.com/10km/sql2java-2-6-7/blob/custom/build.xml
pom.xml完整代碼參見 :https://github.com/10km/sql2java-2-6-7/blob/custom/pom.xml
《maven-antrun-plugin》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.