当前位置:   article > 正文

升级ant配置文件(关于copy maven依赖jar),如何优雅的自动copy scope为system的jar

maven scope system 拷贝

升级ant配置文件(关于copy maven依赖jar),如何优雅的自动copy scopesystemjar

 

(:这个文档需要 antmaven的基础知识)

 

(不想看废话的,直接跳最后面看结论)

 

ant发布的时候copy lib,公司架构师以前是这样来的

 

   <target name="copyLib">

      <artifact:dependencies filesetId="project.dep.fileset" useScope="runtime">

        <pom file="pom.xml" />

      </artifact:dependencies>

 

      <mkdir dir="${deploy.home}/WEB-INF/lib" />

      <copy todir="${deploy.home}/WEB-INF/lib">

        <fileset refid="project.dep.fileset" />

        <mapper type="flatten" />

      </copy>

 

      <copy todir="${deploy.home}/WEB-INF/lib">

        <fileset dir="${basedir}/lib/payment" />

      </copy>                          

   </target>

 

payment 里面 chinabank.jar 等第三方的jar, maven

<scope>system</scope>

<systemPath>${basedir}/lib/payment/chinabank.jar</systemPath>

 

方式进行管理的 ,

 

因为scope system,单独 useScope="runtime" 是不会copy ,因此早期的架构师是这么写的 ,这么写有个局限性,systemjar 必须放在${basedir}/lib/payment 路径下面

 

今天发现 artifact:dependencies 还有个参数 scopes,useScope 的关系是二者选其一,不能同时使用

 

 

useScope 只有三个值,

compile - Includes scopes compile, system and provided
runtime - Includes scopes compile and runtime
test - Includes scopes system, provided, compile, runtime and test

没有我们需要的, test 超过我们需要的, compile runtime  都没有system scope

 

我们使用 scopes (Since 2.0.10) ,支持逗号分隔的scope,如果没有指定值,所有的scope 将被包含

 

ok,那么我们改成

 

   <target name="copyLib">

      <artifact:dependencies filesetId="project.dep.fileset" scopes="compile,runtime,system">

        <pom file="pom.xml" />

      </artifact:dependencies>

 

      <mkdir dir="${deploy.home}/WEB-INF/lib" />

      <copy todir="${deploy.home}/WEB-INF/lib">

        <fileset refid="project.dep.fileset" />

        <mapper type="flatten" />

      </copy>

   </target>

 

这样,我们新增一个system范围的jar,就不用手工的再次修改build.xml运行下看看,看看到底有没有copy 我们的 chinabank.jar

 

检查下,没有!!!!!!! (看来和我们想象的不一样)

 

 

搜了下文档,原来是这样的

 

使用system  scope(指定本机系统的一个路径)的依赖可能在本机maven仓库外面.
一个 Ant fileset 只能允许一个单一的base文件夹,所以这些依赖将不包含在为解决依赖生成的fileset里面
但是被包含在 path 对象里面

 

还好,可以放在pathid 里面

 

尝试下存放到pathid里面,再用for循环取,
(for
属于 ant-contrib,怎么用,自己去研究),妈咪妈咪轰,给个传送门http://www.google.com.hk/search?gcx=c&client=aff-cs-360chromium&ie=UTF-8&q=ant-contrib

 

      <artifact:dependencies pathid="pathSystem" scopes="system">

        <pom file="pom.xml" />

      </artifact:dependencies>

      <for param="mavenSystemJar">

        <path refid="pathSystem" />

        <sequential>

           <echo>@{mavenSystemJar}</echo>

           <copy todir="${deploy.home}/WEB-INF/lib" file="@{mavenSystemJar}" />

        </sequential>

      </for>

 

运行

 

copyLib:

     [echo] E:\dataFixed\.m2\repository\chinabank\chinabank\1.0\chinabank-1.0.jar

 

BUILD FAILED

E:\Workspaces\baozun\nikeplatform\nike-frontend\build.xml:25: The following error occurred while executing this line:

E:\Workspaces\baozun\nikeplatform\nike-frontend\build.xml:29: Warning: Could not find file E:\dataFixed\.m2\repository\chinabank\chinabank\1.0\chinabank-1.0.jar to copy.

 

 

不行,读出来的 jar 路径不是我配置的路径,我并没有deploy repository,我是直接放在项目下面的

 

没辙了,去看看源码吧,看看到底怎么设值的

 

debug 模式运行下  ,看到这样的日志

 

 

Class org.apache.maven.artifact.resolver.ArtifactResolutionResult loaded from ant loader (parentFirst)

Class org.apache.tools.ant.types.FileList loaded from parent loader (parentFirst)

Class org.apache.tools.ant.types.FileSet loaded from parent loader (parentFirst)

Class org.apache.tools.ant.types.PatternSet$NameEntry loaded from parent loader (parentFirst)

Class org.apache.tools.ant.types.FileList$FileName loaded from parent loader (parentFirst)

Setting project property: chinabank:chinabank:jar -> E:\Workspaces\baozun\nikeplatform\nike-frontend\lib\payment\chinabank.jar

Setting project property: cmbc:cmbc:jar -> E:\Workspaces\baozun\nikeplatform\nike-frontend\lib\payment\cmbJava15.jar

Class org.apache.tools.ant.types.Path loaded from parent loader (parentFirst)

Adding reference: pathSystem

Finding class net.sf.antcontrib.logic.ForTask

Loaded from E:\Workspaces\baozun\nikeplatform\nike-frontend\lib\ant\ant-contrib-1.0b3.jar net/sf/antcontrib/logic/ForTask.class

Class net.sf.antcontrib.logic.ForTask loaded from ant loader (parentFirst)

Class org.apache.tools.ant.taskdefs.MacroInstance loaded from parent loader (parentFirst)

Class java.util.ArrayList loaded from parent loader (parentFirst)

Class org.apache.tools.ant.types.DirSet loaded from parent loader (parentFirst)

Class java.util.Iterator loaded from parent loader (parentFirst)

Class java.util.Collection loaded from parent loader (parentFirst)

Class java.lang.Integer loaded from parent loader (parentFirst)

Class org.apache.tools.ant.taskdefs.MacroDef loaded from parent loader (parentFirst)

Class org.apache.tools.ant.taskdefs.MacroDef$Attribute loaded from parent loader (parentFirst)

Class java.io.File loaded from parent loader (parentFirst)

     [echo] E:\dataFixed\.m2\repository\chinabank\chinabank\1.0\chinabank-1.0.jar

     [echo] E:\dataFixed\.m2\repository\cmbc\cmbc\1.0\cmbc-1.0.jar

 

 

可以看到 property 设置的是正确的,但是 取出来的却是错误的 ,哪里出现问题了???

 

再次调式下

 



 

 

会看到 属性已经被设置到 对应的 property 中去了

 

那么我们可以 直接这么取了

 

 

      <artifact:dependencies pathid="pathSystem" scopes="system">

        <pom file="pom.xml" />

      </artifact:dependencies>

      <echo>${cmbc:cmbc:jar}</echo>

      <echo>${chinabank:chinabank:jar}</echo>

      <copy todir="${deploy.home}/WEB-INF/lib" file="${cmbc:cmbc:jar}" />

      <copy todir="${deploy.home}/WEB-INF/lib" file="${chinabank:chinabank:jar}" />

 

运行

 

 

copyLib:

     [echo] E:\Workspaces\baozun\nikeplatform\nike-frontend\lib\payment\cmbJava15.jar

     [echo] E:\Workspaces\baozun\nikeplatform\nike-frontend\lib\payment\chinabank.jar

     [copy] Copying 1 file to E:\home\webuser\appserver\tomcat-nikefront\webapps\nikeshop\WEB-INF\lib

     [copy] Copying 1 file to E:\home\webuser\appserver\tomcat-nikefront\webapps\nikeshop\WEB-INF\lib

 

yes,但是 这不是我要的效果 ,新增一个system 还是得再add 一个 copy

 

再想想其他办法

 

查查文档,写个ant java ,

 

      Project project = new Project();

      project.init();

      ProjectHelper helper = ProjectHelper.getProjectHelper();

      File buildFile = new File("build.xml");

      helper.parse(project, buildFile);

      project.executeTarget("copyLib");

 

调试下

 

 



 

会发现,原来 pathid 设置引用的时候,filelist 就是 使用的 本机仓库的路径,这下悲剧了

 

网上追朔代码,会发现

 

        FileList fileList = new FileList();

        fileList.setDir( getLocalRepository().getPath() );

 

直接就把 dir的路径不分青红皂白()不区分scope设置成本机仓库,  这个我觉得是不正确的做法

 

我用的maven-ant-tasks 版本是2.0.10  (19-May-2009年的),会不会是版本的原因,那么使用个最新的版本(2.1.3)  (13-Apr-2011)看看

 

ant 也从1.6.5 换成1.8.2

 

执行下代码

 

发现果然成功了

 

build.xml代码如下

 

      <artifact:dependencies pathid="pathSystem" scopes="system" verbose="false">

        <pom file="pom.xml" />

      </artifact:dependencies>

      <!--

      <echo>${cmbc:cmbc:jar}</echo>

      <echo>${chinabank:chinabank:jar}</echo>

      <copy todir="${deploy.home}/WEB-INF/lib" file="${cmbc:cmbc:jar}" />

      <copy todir="${deploy.home}/WEB-INF/lib" file="${chinabank:chinabank:jar}" />-->

      <for param="mavenSystemJar">

        <path refid="pathSystem" />

        <sequential>

           <echo>@{mavenSystemJar}</echo>

           <copy todir="${deploy.home}/WEB-INF/lib" file="@{mavenSystemJar}" />

        </sequential>

      </for>

 

 

 



 

 

哦耶~~~~~,很高兴

 

 

 

我们先把 测试的代码封装一下,方便以后调用 (也算代码积累)

 

package com.feilong.tools.ant;

 

import java.io.File;

 

import org.apache.tools.ant.DefaultLogger;

import org.apache.tools.ant.Project;

import org.apache.tools.ant.ProjectHelper;

 

/**

 *ant 工具类

 *

 * @author <a href="mailto:venusdrogon@163.com">金鑫</a>

 * @version 1.0 2011-11-26 下午01:41:31

 */

public final class FeiLongAntUtil{

 

   /**

    * 执行某个任务

    *

    * @param antFilePath

    *            ant文件路径

    * @param targetName

    *            targetName

    * @param messageOutputLevel

    *            日志级别

    */

   public static void executeTarget(String antFilePath,String targetName,int messageOutputLevel){

      Project project = new Project();

      //添加日志输出         

      DefaultLogger consoleLogger = new DefaultLogger();

      consoleLogger.setErrorPrintStream(System.err);

      consoleLogger.setOutputPrintStream(System.out);

      //输出信息级别 

      consoleLogger.setMessageOutputLevel(messageOutputLevel);

      project.addBuildListener(consoleLogger);

      project.init();

      ProjectHelper helper = ProjectHelper.getProjectHelper();

      File buildFile = new File(antFilePath);

      helper.parse(project, buildFile);

      project.executeTarget(targetName);

   }

}

 

调用

 

package com.feilong.tools.ant;

 

import org.apache.tools.ant.Project;

import org.junit.Test;

 

public class FeiLongAntUtilTest{

 

   @Test

   public void executeTarget(){

      String file = "build.xml";

      String targetName = "copyLib";

      int messageOutputLevel = Project.MSG_DEBUG;

      FeiLongAntUtil.executeTarget(file, targetName, messageOutputLevel);

   }

}

 

看看源码 org.apache.maven.artifact.ant.DependenciesTask.class 改动很大,不是一般的大,路径代码都重构了

 

 

再看看 release log  http://maven.apache.org/ant-tasks/release-notes.html

 

2.1.0 Release Notes

The full list of changes can be found in our issue management system, and is reproduced below.

Bug
  • [MANTTASKS-4] - System scope not working properly in Maven Antlib
  • [MANTTASKS-152] - Mvn task omit localRepository param
  • [MANTTASKS-153] - Properties defined in Ant are not passed to Maven
  • [MANTTASKS-155] - Dependency fileset should set the current ant project
  • [MANTTASKS-159] - Wrong credentials used for mirrored repositories
  • [MANTTASKS-160] - Wrong metadata files created for mirrored remote repositories
  • [MANTTASKS-167] - Generated ant build file should save version list for version mapper
Improvement
  • [MANTTASKS-117] - Setting Maven user-properties not possible
  • [MANTTASKS-169] - Dependencies "verbose" option should be deprecated in favor of standard ant verbose option
New Feature
  • [MANTTASKS-151] - Support for artfacts that are 'classified' from birth
  • [MANTTASKS-156] - Add feature to dependencies task to write file paths to a file.
  • [MANTTASKS-168] - New task to write a pom file

看到没有,2.1.0修复了刚才的bug

 

 

好了总结下(有如下改动):

 

1. <path id="maven-ant-tasks.classpath" path="${basedir}/lib/ant/maven-ant-tasks-2.0.10.jar" />

 

改成   <path id="maven-ant-tasks.classpath" path="${basedir}/lib/ant/maven-ant-tasks-2.1.2.jar" />

 

 

2. copyLib 改成如下

 

   <target name="copyLib">

      <!---->

      <artifact:dependencies filesetId="project.dep.fileset" scopes="compile,runtime">

        <pom file="pom.xml" />

      </artifact:dependencies>

 

      <artifact:dependencies pathid="pathSystem" scopes="system">

         <pom file="pom.xml" />

      </artifact:dependencies>

     

      <property name="deploy.lib" value="${deploy.home}/WEB-INF/lib"></property>

     

      <mkdir dir="${deploy.lib}" />

      <!--

      <echo>${cmbc:cmbc:jar}</echo>

      <echo>${chinabank:chinabank:jar}</echo>

      <copy todir="${deploy.home}/WEB-INF/lib" file="${cmbc:cmbc:jar}" />

      <copy todir="${deploy.home}/WEB-INF/lib" file="${chinabank:chinabank:jar}" />-->

      <for param="mavenSystemJar">

        <path refid="pathSystem" />

        <sequential>

           <echo>@{mavenSystemJar}</echo>

           <copy todir="${deploy.lib}" file="@{mavenSystemJar}" />

        </sequential>

      </for>

 

      <!---->

      <copy todir="${deploy.lib}">

        <fileset refid="project.dep.fileset" />

        <mapper type="flatten" />

      </copy>

   </target>

 

收工,以后不管怎么加jar,都不需要改动ant

 

这次改动中学习了很多ant maven的高级知识,收获不小, 浪里格朗~~~

 

 

 

参考资料:

http://maven.apache.org/ant-tasks/index.html

http://maven.apache.org/ant-tasks/release-notes.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/308055
推荐阅读
相关标签
  

闽ICP备14008679号