Maven 调用 ant进行编译!!

之所以采用Maven调用ant 进行编译,是因为有一些lib 并不在远程仓库里,而是直接放到project/lib目录下。

 

编译的时候要依赖这些lib包, maven里好像没什么办法把这些非仓库里的lib包加入到classpath中来,才采用调用ant的方式。

 

ps: 但是如果要采用这种把非仓库lib的加入到classpath的方式,就无法使用maven官方推荐的maven-ant-tasks进行抽取pom的dependency,所以我在这个例子中把那段注释掉了。。

这是不是一个maven的bug?

 

 

 

----------------------------------------------------------------------------

Pom.xml:

 

<build>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.3</version>

   <executions>
    <execution>
     <id>ant-build</id>
     <phase>generate-sources</phase>
     <goals>
      <goal>run</goal>
     </goals>
     <configuration>
      <tasks>
       <property name="compile_classpath" refid="maven.compile.classpath" />
       <property name="runtime_classpath" refid="maven.runtime.classpath" />
       <property name="test_classpath" refid="maven.test.classpath" />
       <property name="plugin_classpath" refid="maven.plugin.classpath" />
       <property name="artifactId" value="${project.artifactId}" /> 
       <property name="version" value="${project.version}" />
       <property name="build.compiler" value="extJavac"/>                        
       <ant antfile="build.xml" />
      </tasks>
     </configuration>
    </execution>
   </executions>
  </plugin>
 </plugins>
</build>

 

-------------------------------------------------------------------------------------------------------------------

build.xml

 

<project name="uslaminstallerjavaproject" default="dist" basedir="." xmlns:artifact="urn:maven-artifact-ant">

 <property environment="env" />
 <property name="vendor" value="HP Company." />
 <property name="work.src" value="${basedir}/src/main/java" />
 <property name="work.conf" value="${basedir}/src/main/resources" />
 <property name="work.lib" value="${basedir}/lib" />
 <property name="work.dist" value="${basedir}/target" />
 <property name="work.classes" value="${basedir}/target/classes" />

 <echo message="compile classpath: ${compile_classpath}" />
 <echo message="runtime classpath: ${runtime_classpath}" />
 <echo message="test classpath:    ${test_classpath}" />
 <echo message="plugin classpath:  ${plugin_classpath}" />

 
 <target name="clean" description="Delete old build and dist directories">
  <echo message=" Clean the classe directory" />
  <delete dir="${work.classes}" />
  <delete dir="${work.dist}" />
 </target>

 <target name="mkdir" description="Make build and dist directories">
  <echo message=" Make the classe directory" />
  <mkdir dir="${work.classes}" />
  <mkdir dir="${work.dist}" />
 </target>

 

<!-- 但是如果要采用这种把非仓库lib的加入到classpath的方式,就无法使用maven官方推荐的maven-ant-tasks进行抓取dependency -->

 <!--
 <target name="maven-jar" description="Use Maven2 to manage jars' dependencies">
  <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
   <classpath>
    <pathelement location="lib/maven-ant-tasks-2.0.9.jar" />
   </classpath>
  </typedef>

  <artifact:pom id="maven.project" file="pom.xml"  />

  <echo message="The build directory is ${maven.project.build.directory}" />

  <artifact:dependencies pathId="maven.classpath" filesetId="maven.deps.fileset" usescope="runtime">
   <pom refid="maven.project" />
  </artifact:dependencies>
 </target>
 -->

 <path id="project.classpath">
  <dirset dir="${work.conf}"/>
  <fileset dir="${work.lib}">
   <include name="**/*.jar"/>
   <include name="**/*.zip"/>
  </fileset>
  <pathelement path="${compile_classpath}"/>
  <!--<fileset refid="maven.deps.fileset" /> -->
 </path>

 <target name="compile" description="compiles all source files">
  <echo message=" Start to compile..." />
  <javac srcdir="${work.src}" destdir="${work.classes}" debug="true">
   <classpath refid="project.classpath" />
  </javac>
  <echo message=" Finish to compile..." />
 </target>

 <target name="jar-all" description="Jar the classes">
  <jar destfile="${work.dist}/${artifactId}-${version}.jar" 
             basedir="${work.classes}" encoding="UTF-8">

   <fileset dir="${work.conf}">
    <include name="**/*.*"/>
   </fileset>
 
   <manifest>
    <attribute name="Implementation-Title" 
                     value="${artifactId}" />
    <attribute name="Implementation-Version" 
                     value="${version}" />
    <attribute name="Implementation-Vendor" 
                     value="${vendor}" />
   </manifest>
  </jar>
 </target>
 
 <target name ="zip-release" description="Zip the final release file">
    <zip destfile="${work.dist}/${artifactId}-${version}.zip">
      <zipfileset dir="${work.dist}" includes="**/*.jar"/>
      <zipfileset dir="${work.lib}" includes="**/*.jar" excludes= "maven-ant-tasks*.jar" />
    </zip>
 </target>

 <target name="dist" depends="clean,mkdir,compile,jar-all">
  <antcall target="zip-release">
  </antcall>
 </target>
</project>

 

 

你可能感兴趣的:(Maven 调用 ant进行编译!!)