估计我是喜欢上转载东西了。这两天开始学习Maven。。。看了一些资料,结合项目中POM.xml。。。算是了解下皮毛。
1.Maven是什么?
2.本地仓库(localRepository)
3.Pom文件
groupId:artifactId:packaging:version(mavenbook:my-app:jar:1.0-SNAPSHOT.)
mvn archetype:create /-DgroupId=my.test.maven
/-DartifactId=demo.web
/-DpackageName=my.test
/-DarchetypeArtifactId=maven-archetype-webapp
my.test.maven
demo.web
war
1.0-SNAPSHOT
依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<debug>true</debug>
<failOnError>false</failOnError>
<executable>${JAVA_HOME}/bin/javac</executable>
</configuration>
</plugin>
4.经常使用的Maven命令
mvn archetype:create
-DgroupId=packageName
-DartifactId=projectName
2. 创建Maven的Web项目:
mvn archetype:create
-DgroupId=packageName
-DartifactId=webappName
-DarchetypeArtifactId=maven-archetype-webapp
3. 编译源代码: mvn compile
4. 编译测试代码:mvn test-compile
5. 运行测试:mvn test
6. 产生site:mvn site
7. 打包:mvn package
8. 清除产生的项目:mvn clean
9. 生成eclipse项目:mvn eclipse:eclipse
10. 编译测试的内容:mvn test-compile
11. 只打jar包: mvn jar:jar
12. 当开发一个带有很多失败单元测试的系统
mvn test -Dmaven.test.failure.ignore=true
13. 想要整个的跳过测试
mvn install -Dmaven.test.skip=true
5、 Assembly插件 maven assembly plugin
mvn archetype:generate //创建 Maven 项目
mvn compile //编译源代码
mvn test-compile //编译测试代码
mvn test //运行应用程序中的单元测试
mvn site //生成项目相关信息的网站
mvn clean //清除目标目录中的生成结果
mvn package //依据项目生成 jar 文件 【一般直接使用: mvn clean package 】
mvn install //在本地 Repository 中安装 jar
mvn eclipse:eclipse //生成 Eclipse 项目文件
需要在eclipse中,配置环境变量M2_REPO,Window->Preferences->Java->Build Path->Classpath Variables
JAVA项目 mvn archetype:create -DgroupId=net.welken -DartifactId=common
web 项目 mvn archetype:create -DgroupId=net.welken -DartifactId=web-app -DarchetypeArtifactId=maven-archetype-webapp 【创建项目】
命令行先执行:set MAVEN_OPTS=-Xms256m -Xmx256m,然后照常执行:mvn install.问题解决!
src/main/java :是项目编译时默认的编译文件位置。
src/main/resources :是项目资源文件默认存放的位置。
对应POM.xml 的:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
POM.xml会下载项目需要的包:
<dependency>
<groupId>net.sf.json-lib</groupId> //对应.m2/repository/net/sf/json-lib/json-lib/2.3/json-lib-2.3.jar
<artifactId>json-lib</artifactId>
<version>2.3</version>
<classifier>jdk15</classifier>
</dependency>
命令:mvn eclipse:eclipse 编译生成eclipse项目,便可导入我们的开发环境中。
命令:mvn clean package 包括编译,,,打包。所有生成文件都在target目录下。