0
点赞
收藏
分享

微信扫一扫

学习Maven的使用


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

手动增加一个jar到Maven的本地仓库:
[b][color=red]mvn install:install-file -Dfile=jms-1.1.jar -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar[/color][/b]
比如增加sqlserver driver到仓库:
先重新命名:sqljdbc4.jar --> sqljdbc-4.0.jar
[b]mvn install:install-file -Dfile=D:/databaseDrivers/sqljdbc-4.0.jar -DgroupId=sqljdbc -DartifactId=sqljdbc -Dversion=4.0 -Dpackaging=jar[/b]
就可以在pom.xml使用

<dependency>
			<groupId>sqljdbc</groupId>
			<artifactId>sqljdbc</artifactId>
			<version>4.0</version>
		</dependency>


自己的一个实例:


[b]1. mvn archetype:create -DgroupId=com.pandy -DartifactId=study[/b]


建立工程项目,得到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>




[b]2. cd study[/b]


进入工程文件夹



[b]3. mvn compile[/b]


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



[b]4. mvn package[/b]


打包



[b]5. mvn clean[/b]


清除之前刚生成的东西,



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


需要做一些另外的事情,然后再打包,得到的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>




[b]6. mvn assembly:assembly[/b]


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




[color=red]使用maven插件得到项目依赖的库文件[/color]


插件名称:maven-dependency-plugin。网址: [url]http://maven.apache.org/plugins/maven-dependency-plugin[/url]


[color=red]从Maven仓库中导出jar包:[/color]进入工程pom.xml 所在的目录下,


有时需要得到当前项目所有依赖的jar文件,可以执行这个goal:copy-dependencies


输入以下命令:


1.[b]mvn dependency:copy-dependencies -DoutputDirectory=lib[/b] 会导出到target/lib 下面

2. [b][color=red]mvn dependency:copy-dependencies[/color][/b] 会导出到target/dependency 下面


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


[b]mvn dependency:copy-dependencies -DoutputDirectory=C:/lib -DincludeScope=compile [/b]这个试过了可以。

这样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>

举报

相关推荐

0 条评论