您的位置:首页 > 编程语言 > Java开发

JAVA中MAVEN的使用

2016-02-17 00:00 555 查看
MAVEN可以方便的进行JAVA项目的管理,创建,包括部署到tomcat,都很便捷

一、MAVEN环境以及maven eclipse插件安装

安装方法参照:
http://blog.csdn.net/maosijunzi/article/details/21160965
在这里需要说明的一点是,我们在运行maven的时候,会发现每次都要重新下载一遍依赖库,原因就是我们的maven没本地化,如下是本地化方法:

1、修改maven的settings.xml【我的在D:\maven\apache-maven-3.3.3\conf目录】

<localRepository>D:/maven/apache-maven-3.3.3/repository/</localRepository>

2、修改eclipse的maven的插件为本地

window->preferences->Maven选择Installations

添加一个外部【external】的maven,Installation home选择我们安装maven的位置,我的是【D:\maven\apache-maven-3.3.3】

window->preferences->Maven选择User Settings

将User Settings【是User而不是Global Settings,不然改了我们在java build path看到的M2_REPO还是默认地址】 选择为D:\maven\apache-maven-3.3.3\conf\settings.xml可以看到下面的Local Repository为我们上面设置的。

3、打开命令行,输入mvn help:system

mvn dependency:sources

如下是常用的Maven命令:
mvn archetype:create :创建 Maven 项目
mvn compile :编译源代码
mvn test-compile :编译测试代码
mvn test : 运行应用程序中的单元测试
mvn site : 生成项目相关信息的网站
mvn clean :清除目标目录中的生成结果
mvn package : 依据项目生成 jar 文件
mvn install :在本地 Repository 中安装 jar
mvn eclipse:eclipse :生成 Eclipse 项目文件
mvn -Dmaven.test.skip=true : 忽略测试文档编译

==============================

mvn eclipse:clean
mvn eclipse:eclipse
mvn help:system

4、部署到tomcat

在部署之前,必须先启动tomcat7服务,C:\Program Files\apache-tomcat-7.0.39\bin\startup.bat

找到要部署的工程文件根目录下,执行如下maven命令

> mvn clean:install   //clean是清理输出文件,install编译打包,在每次打包之前必须执行clean,才能保证发布为最新文件

> mvn tomcat7:redeploy   //第一次发布 tomcat7:deploy,再次发布 tomcat7:redeploy

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</dependency>

<repositories>
<repository>
<id>people.apache.snapshots</id>
<url> http://repository.apache.org/content/groups/snapshots-group/ </url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<name>Apache Snapshots</name>
<url> http://repository.apache.org/content/groups/snapshots-group/ </url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

将如下的加入到build里面,用于支持tomcat6,tomcat7部署:

<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>tomcat</server>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>tomcat7</server>
<username>admin</username>
<password>123456</password>
</configuration>
</plugin>
</plugins>
</build>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: