学习Maven的使用

学习maven的使用: http://fluagen.blog.51cto.com/146595/40086
Maven 试用手记----开始一个新的项目并编译和测试: http://www.blogjava.net/lvdougao/articles/26827.html
maven 编译命令: http://radio123.iteye.com/blog/1490335
maven中文教程——一个简单的应用实例: http://www.zhlwish.com/2009/11/15/maven-lesson-simple-example/
APACHE的MAVEN管理jar包: http://www.iteye.com/topic/221543
从Maven仓库中导出jar包: http://blog.csdn.net/jiafu1115/article/details/7480015

如何将maven项目打包成可执行的jar http://blog.csdn.net/symgdwyh/article/details/6081532
手动增加一个jar到Maven的本地仓库:
mvn install:install-file -Dfile=jms-1.1.jar -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar
比如增加sqlserver driver到仓库:
先重新命名:sqljdbc4.jar --> sqljdbc-4.0.jar
mvn install:install-file -Dfile=D:/databaseDrivers/sqljdbc-4.0.jar -DgroupId=sqljdbc -DartifactId=sqljdbc -Dversion=4.0 -Dpackaging=jar
就可以在pom.xml使用
		<dependency>
			<groupId>sqljdbc</groupId>
			<artifactId>sqljdbc</artifactId>
			<version>4.0</version>
		</dependency>




自己的一个实例:
1. mvn archetype:create -DgroupId=com.pandy -DartifactId=study
建立工程项目,得到pom.xml
<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.pandy</groupId>
	<artifactId>study</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>study</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>



2. cd study
进入工程文件夹

3. mvn compile
编译java,这次编译成功过了哦。运行“mvn package”,在target目录下生成了jpastudy-1.0-SNAPSHOT.jar,但是我们还不能直接运行这个jar,因为所有的运行时依赖没有加入到classpath中。

4. mvn package
打包

5. mvn clean
清除之前刚生成的东西,

-------------------------------------------------------------------
需要做一些另外的事情,然后再打包,得到的pom.xml如下:
--------------------------------------
<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.pandy</groupId>
	<artifactId>study</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>study</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<!-- 增加spring-webmvc依赖 -->
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.1.2.RELEASE</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<!-- 注解支持,jdk5才具有的新特性,我们需要设置compile插件,具体可以参考Setting the -source and -target of the Java Compiler,根据说明,我们继续向pom文件中加入 -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>5</source>
					<target>5</target>
				</configuration>
			</plugin>

			<plugin>
				<!-- 
				我们想将所有的依赖库都打包,直接交给用户,这样用户不需要在做其他设置了,这里需要使用Assembly插件了,其说明参考Pre-defined Descriptor Files,这个参考文件也说明了有四种默认定义的打包方式,我们选择jar-with-dependencies,继续添加pom文件
				-->
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>
			</plugin>

		</plugins>
	</build>
</project>



6. mvn assembly:assembly
然后运行“mvn assembly:assembly”, 在target目录中生成了jpastudy-1.0-SNAPSHOT-jar-with-dependencies.jar


使用maven插件得到项目依赖的库文件
插件名称:maven-dependency-plugin。网址: http://maven.apache.org/plugins/maven-dependency-plugin


从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,
有时需要得到当前项目所有依赖的jar文件,可以执行这个goal:copy-dependencies
输入以下命令:
1. mvn dependency:copy-dependencies -DoutputDirectory=lib 会导出到target/lib 下面
2. mvn dependency:copy-dependencies    会导出到target/dependency 下面

mvn clean dependency:copy-dependencies package  未试过,复制依赖的jar 非常有用

mvn dependency:copy-dependencies -DoutputDirectory=C:/lib   -DincludeScope=compile 这个试过了可以。
这样jar包都会copy到工程目录下的lib里面

mvn package
然后从被打包好的文件里找出jar来。


	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-dependency-plugin</artifactId>
		<executions>
			<execution>
				<id>copy</id>
				<phase>install</phase>
				<goals>
					<goal>copy-dependencies</goal>
				</goals>
				<configuration>
					<outputDirectory>$/lib</outputDirectory>
					<!-- 拷贝所以依赖存放位置 -->
				</configuration>
			</execution>
		</executions>
	</plugin>

猜你喜欢

转载自panyongzheng.iteye.com/blog/1759912