ant的项目如何利用maven来管理依赖
·
我们的目标
1. 对喜欢maven的人, 能快速利用maven创建新的项目, 而不用痛苦地去一个一个找依赖, 作为普通开发者可以不关心复杂的build.xml, 或者直接用maven构建
2. 对原本使用ant脚本的项目, 在要求兼容maven的情况下, 仅仅添加maven的依赖管理, 同时可以为ant提供依赖包的自动收集, 不需要手动自己找.
3. 推行maven的使用, 快速创建开发环境.
4. subversion不在保存一堆一堆巨大的jar包
本文的内容主要是针对上面的第二条, 当然最终是希望推行maven, 方便管理.
首先我们要安装好ant, maven2, 配置好环境变量, 修改maven的缓存路径.
然后安装 http://maven.apache.org/ant-tasks/index.html 包, 可以直接放在ant的lib目录下, 当然还有其他方式, 例如在build.xml里指定路径.
准备pom.xml, 针对本文所说情况, 只需要写基本信息和依赖包配置即可.
举例:
如果有本地仓库, 还可以配置repository. (推荐优先使用本地服务器), 这个文件内容很少, 主要是依赖包的信息, 也是我们最关注的信息.
下面我们来修改我们的build.xml, 为ant提供依赖服务:
<project name="jgroups" basedir="." xmlns:artifact ="urn:maven-artifact-ant" >
<!-- 存放依赖包的目录 -->
<property name="build.lib.dir" location="./lib" />
<!-- find maven-ant-tasks, you can use classpathref to specify the jar, or just copy the jar to ant/lib -->
<typedef resource ="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant" />
<!-- maven的文件 -->
<artifact:pom id ="maven.project" file ="pom.xml" />
<artifact:dependencies filesetId="deps.fileset.compile" useScope="compile">
<!--<pom file="pom.xml"/>-->
<pom refid="maven.project" />
</artifact:dependencies>
<artifact:dependencies filesetId="deps.fileset.test" scopes="test">
<pom refid="maven.project" />
</artifact:dependencies>
<artifact:dependencies filesetId="deps.fileset.provide" scopes="provide">
<pom refid="maven.project" />
</artifact:dependencies>
<target name="lib.init">
<copy todir="${build.lib.dir}/compile">
<fileset refid="deps.fileset.compile"/>
<mapper type="flatten"/>
</copy>
<copy todir="${build.lib.dir}/test">
<fileset refid="deps.fileset.test"/>
<mapper type="flatten"/>
</copy>
<copy todir="${build.lib.dir}/provide">
<fileset refid="deps.fileset.test"/>
<mapper type="flatten"/>
</copy>
</target>
<!--
..其他

.. -->
</project>
主要增加了一个target, 就是把maven中定义的依赖包复制到指定目录下, 而不需要自己准备或者从subversion下载.
(ant的其他target也可使用定义的几个变量作为classpath)
一般依赖分三种, 请注意区分, 否则把servlet等jar包发布到服务器, 一般会出错:
使用方法:
1. 对喜欢maven的人, 能快速利用maven创建新的项目, 而不用痛苦地去一个一个找依赖, 作为普通开发者可以不关心复杂的build.xml, 或者直接用maven构建
2. 对原本使用ant脚本的项目, 在要求兼容maven的情况下, 仅仅添加maven的依赖管理, 同时可以为ant提供依赖包的自动收集, 不需要手动自己找.
3. 推行maven的使用, 快速创建开发环境.
4. subversion不在保存一堆一堆巨大的jar包
本文的内容主要是针对上面的第二条, 当然最终是希望推行maven, 方便管理.
首先我们要安装好ant, maven2, 配置好环境变量, 修改maven的缓存路径.
然后安装 http://maven.apache.org/ant-tasks/index.html 包, 可以直接放在ant的lib目录下, 当然还有其他方式, 例如在build.xml里指定路径.
准备pom.xml, 针对本文所说情况, 只需要写基本信息和依赖包配置即可.
举例:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.test</groupId>
<artifactId>maventest</artifactId>
<name>Maven Test</name>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>2.10.0.GA</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<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>com.test</groupId>
<artifactId>maventest</artifactId>
<name>Maven Test</name>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>2.10.0.GA</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
如果有本地仓库, 还可以配置repository. (推荐优先使用本地服务器), 这个文件内容很少, 主要是依赖包的信息, 也是我们最关注的信息.
下面我们来修改我们的build.xml, 为ant提供依赖服务:
<project name="jgroups" basedir="." xmlns:artifact ="urn:maven-artifact-ant" >
<!-- 存放依赖包的目录 -->
<property name="build.lib.dir" location="./lib" />
<!-- find maven-ant-tasks, you can use classpathref to specify the jar, or just copy the jar to ant/lib -->
<typedef resource ="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant" />
<!-- maven的文件 -->
<artifact:pom id ="maven.project" file ="pom.xml" />
<artifact:dependencies filesetId="deps.fileset.compile" useScope="compile">
<!--<pom file="pom.xml"/>-->
<pom refid="maven.project" />
</artifact:dependencies>
<artifact:dependencies filesetId="deps.fileset.test" scopes="test">
<pom refid="maven.project" />
</artifact:dependencies>
<artifact:dependencies filesetId="deps.fileset.provide" scopes="provide">
<pom refid="maven.project" />
</artifact:dependencies>
<target name="lib.init">
<copy todir="${build.lib.dir}/compile">
<fileset refid="deps.fileset.compile"/>
<mapper type="flatten"/>
</copy>
<copy todir="${build.lib.dir}/test">
<fileset refid="deps.fileset.test"/>
<mapper type="flatten"/>
</copy>
<copy todir="${build.lib.dir}/provide">
<fileset refid="deps.fileset.test"/>
<mapper type="flatten"/>
</copy>
</target>
<!--

..其他

.. --></project>
主要增加了一个target, 就是把maven中定义的依赖包复制到指定目录下, 而不需要自己准备或者从subversion下载.
(ant的其他target也可使用定义的几个变量作为classpath)
一般依赖分三种, 请注意区分, 否则把servlet等jar包发布到服务器, 一般会出错:
- compile: 编译, 用于运行时和编译时, 打包发布要包含
- provide: 容器提供的类, 主要用于编译, 打包发布时不包含, 例如servlet-api
- test: 仅用于编译和运行测试用例, 打包时不包含.
使用方法:
- checkout项目
- 使用ide的方式导入maven项目. 如果需要手动: 运行ant lib.init (如果下载jar时包出错, 注意检查groupId, artifactId和版本号.)
- 手动创建项目, 指定library, 就不需要一个一个找了. (如果导入maven项目的方式, 则一切自动会配置好)
更多推荐
所有评论(0)