刚到公司,部门中导师让我学习一下maven,网上找到IBM的教程,按照步骤,先在命令行中操作,然后在eclipse 里操作一遍,已经有初步了解。下面将原文主要内容整理下来,原文参见网址http://www.ibm.com/developerworks/cn/education/java/j-mavenv2/index.html。其中部分地方有误,可以参看下文解决问题。
1. 新建一个项目,groupid = com.lzy.mvntest , artifactId为NumOps,和版本号一起唯一标识工程。
在命令行下 mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.lzy.mvntest -DartifactId=NumOps
该命令为 Archetype 插件提供您模块的坐标:com.ibm.devworks/NumOps/1.0-SNAPSHOT。
在此情况下,不需要指定版本,因为 Archetype 插件常默认为 1.0-SNAPSHOT。
此命令为项目创建了一个起始的 pom.xml 文件,也创建了规范的 Maven 2 目录结构。
2. 写好对应的java文件以后,在pom.xml文件里面加入以下build信息
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>(这个地方没有加上的话会报错,参考http://maven.apache.org/plugins/maven-compiler-plugin/usage.html)
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
进入到项目文件夹中使mvn compile进行编译
3. 在对应的test目录下面写测试文件,使用命令mvn test进行编译和测试
4. 在项目整合的时候, 每一个均有自己的pom.xml文件,同时在最顶层有主项目的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.devworks</groupId>
<artifactId>mavenex2</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Example 2</name>
<url>http://maven.apache.org</url>
<modules>
<module>NumOps</module>
<module>OpsImp</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.ibm.devworks</groupId>
<artifactId>OpsImp</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
其中对于junit的依赖,模块中不用重复写,因为继承了主模块的属性。
<modules>标签和打包类型为pom表示这个是一个多模块的项目
<dependencyManagement> 标记不指定此模块依赖的依赖项。
相反,它主要由子模块使用。
子模块能指定 <dependencyManagement> 标记中任何条目的依赖项,而无需指定具体的版本号。
当项目树更改依赖项的版本号时,这很有用,可以使需编辑的条目数目最小化。
在本例中,OpsImp 项目的版本号是使用 ${project.version} 指定的。
在执行 Maven 时,这是一个会被相应值所填充的参数。
5. 使用mvn test 或者mvn complie进行编译,使用mvn install进行打包。
使用mvn help:effective-pom 显示集成后的pom的内容 。
6. eclipse的maven的插件安装以及使用参考命令行下的步骤后较为简单,在此不记录。
7. 因为依赖的组件大部分是在组件库中,有时需要手动添加组件进来。在eclipse里面先要导入自己工程依赖的组件,然后再添加依赖项的功能中找到对应的。具体方法是在项目点右键,import->install or deploy an artifact to a Maven repository,添加后,再项目点右键->maven->add dependency输出刚才导入的依赖组件即可。
如果要手动输入的话按照以下命令:
mvn install:install-file -Dfile=sqljdbc.jar -DgroupId=com.lee -DartifactId=sqljdbc -Dversion=5.1.18 -Dpackaging=jar
其中Dfile是jar包路径,依次是groupid,artifactid和版本号以及打包方式。 执行命令后需要手动将其拷入到本地仓库中,仓库地址参考maven的配置文件。然后在文件中加入相关的dependency标签或者项目即可。
1.Configure Maven environment
1) Download Maven
http://maven.apache.org/download.html
apache-maven-3.0.4-bin.tar.gz
http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.tar.gz
2) Unzip apache-maven-3.0.4-bin.tar.gz to C:\CI
3) My Computer --> Property --> Environment Variant--> System Variant-->
Add-->name: M2_HOME value: C:\CI\apache-maven-3.0.4-->OK-->
choose Path-->edit-->add the last: ;%M2_HOME%\bin-->OK-->OK-->OK
4) Test the Maven
cmd--> mvn -v
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: C:\CI\apache-maven-3.0.4\bin\..
Java version: 1.6.0_31, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_31\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
It shows it is ok.
2.Configure a Java and Maven project : Hello-world (Non-Eclipse IDE)
1) Create a hello-world folder
2) Create a file named pom.xml under hello-world folder
3) Edit 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.juvenxu.mvnbook</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
</project>
4) Create src/main/java folder under hello-world folder
5) Create file com/juvenxu/mvnbook/helloworld/HelloWorld.java under hello-world/src/main/java folder
6) Eidt HelloWorld.java :
package com.juvenxu.mvnbook.helloworld;
public class HelloWorld
{
public String sayHello()
{
return "Hello Maven";
}
public static void main(String[] args)
{
System.out.println(new HelloWorld().sayHello());
}
}
7) cmd ---> cd /d (To hello-world folder ) -->
F:\hello-world>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-world ---
[INFO] Deleting F:\hello-world\target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-world ---
[debug] execute contextualize
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform
dependent!
[INFO] skip non existing resourceDirectory F:\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform depend
ent!
[INFO] Compiling 1 source file to F:\hello-world\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.636s
[INFO] Finished at: Fri Feb 24 09:54:39 CST 2012
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
F:\hello-world>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-world ---
[INFO] Deleting F:\hello-world\target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-world ---
[debug] execute contextualize
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\hello-world\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.639s
[INFO] Finished at: Fri Feb 24 09:54:51 CST 2012
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
F:\hello-world>
7) Add Test into project,Edit the 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.juvenxu.mvnbook</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
8) Create src/test/java folder under hello-world folder
9) Create file com/juvenxu/mvnbook/helloworld/HelloWorldTest.java under hello-world/src/test/java folder
10) Eidt HelloWorldTest.java :
package com.juvenxu.mvnbook.helloworld;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloWorldTest
{
@Test
public void testSayHello()
{
HelloWorld helloWorld = new HelloWorld();
String result = helloWorld.sayHello();
assertEquals("Hello Maven",result);
}
}
11) cmd ---> cd /d (To hello-world folder ) -->
F:\hello-world>mvn clean test
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.075 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.241s
[INFO] Finished at: Fri Feb 24 09:58:10 CST 2012
[INFO] Final Memory: 9M/21M
[INFO] ------------------------------------------------------------------------
F:\hello-world>
11) cmd ---> cd /d (To hello-world folder ) -->
F:\hello-world>mvn clean package
...
[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ hello-world ---
[INFO] Building jar: F:\hello-world\target\hello-world-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.701s
[INFO] Finished at: Fri Feb 24 09:59:10 CST 2012
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
12) cmd ---> cd /d (To hello-world folder ) -->
F:\hello-world>mvn clean install
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ hello-world ---
[INFO] Building jar: F:\hello-world\target\hello-world-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ hello-world ---
[INFO] Installing F:\hello-world\target\hello-world-1.0-SNAPSHOT.jar to C:\Users\NHN\.m2\repository\com\juvenxu\mvnbook\
hello-world\1.0-SNAPSHOT\hello-world-1.0-SNAPSHOT.jar
[INFO] Installing F:\hello-world\pom.xml to C:\Users\NHN\.m2\repository\com\juvenxu\mvnbook\hello-world\1.0-SNAPSHOT\hel
lo-world-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.946s
[INFO] Finished at: Fri Feb 24 10:00:42 CST 2012
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
13)Configure executable jar, Edit the 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.juvenxu.mvnbook</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
14) cmd ---> cd /d (To hello-world folder ) -->
F:\hello-world>mvn clean install
...
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ hello-world ---
[INFO] Installing F:\hello-world\target\hello-world-1.0-SNAPSHOT.jar to C:\Users\NHN\.m2\repository\com\juvenxu\mvnbook\
hello-world\1.0-SNAPSHOT\hello-world-1.0-SNAPSHOT.jar
[INFO] Installing F:\hello-world\pom.xml to C:\Users\NHN\.m2\repository\com\juvenxu\mvnbook\hello-world\1.0-SNAPSHOT\hel
lo-world-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.888s
[INFO] Finished at: Fri Feb 24 10:02:25 CST 2012
[INFO] Final Memory: 9M/24M
[INFO] ------------------------------------------------------------------------
15)cmd ---> cd /d (To hello-world folder ) -->
F:\hello-world>java -jar target\hello-world-1.0-SNAPSHOT.jar
Hello Maven
16) put hello-world folder in svn server-->Tortoise SVN-->Repo-browser-->
URL: http://localhost/svn/myweb/trunk/-->OK-->Username: admin-->Password: admin-->
Save authentication-->OK-->Create folder...-->New name: MyMaven-->OK-->OK-->Tow the hello-world folder to the MyMaven folder-->
Question: NO
Commit person: CN15209
Commit reason: Commit the hello-world project.
-->OK-->OK-->delete the local hello-world folder-->
SVN Checkout...-->URL of repository:http://localhost/svn/myweb/trunk/MyMaven/hello-world-->
OK-->
Now we create a repository on svn.
17) start hudson(I use Java Method to start hudson)
18) firefox-->http://localhost:8080 --> Manage Hudson-->Configure System-->JDK-->
Add JDK-->Name: JDK1.6.0_31-->JAVA_HOME: C:\Program Files\Java\jdk1.6.0_31-->Maven-->Add Maven-->
Name: M2-->cancel Install automatically-->MAVEN_HOME: C:\CI\apache-maven-3.0.4-->
E-mail Notification--> Advanced-->
SMTP server : 127.0.0.1
Default User E-mail Suffix: @mymail.cn
System Admin E-mail Address: Jenny@mymail.cn
Hudson URL: http://localhost:8080/
Use SMTP Autherntication (Choose)
User Name: Jenny@mymail.cn
Password: Jenny
User SSL (Disable)
SMTP Port: 25
Charset UTF-8
-->Test configuration by sending e-mail to System Admin Address-->Save-->Your Foxmail will receive a email from hudson.It's said that it is ok.
-->Save
19) Hudson-->New Job-->Job name: myfirstmavenjob-->choose Build a Maven 2/3 project(Legacy)-->OK-->
Source Code Management choose Subversion-->Repository URL: http://localhost/svn/myweb/trunk/MyMaven/hello-world-->
enter credential-->User name/password authentication-->User name: admin-->Password: admin-->ok-->close-->
Build-->Goals and options: clean install
-->Save-->Hudson-->choose myfirstmavenjob Schedule a build-->
See Latest Console output
20) Create trigger -->Hudson-->myfirstmavenjob-->Configure-->Choose Poll SCM-->Schedule: * * * * * (Every minute to check codes)-->Save
#########################
Here you maybe use internet,but I haven't it.So I setup a mail server and foxmail client.
Please See my hMailServer_Step by Step.txt file for the detail.
I configure 127.0.0.1 smtp server ip,User : Jenny@mymail.cn Password : Jenny
#########################
21) Hudson-->Manage Hudson-->Configure System-->E-mail Notification-->Recipients:Jenny@mymail.cn-->
Choose Send e-mail for every unstable build
Don't Choose Send separate e-mails to individuals who broke the build (because the svn user isn't a email user,so if you choose this ,the email
can't send above the correct email user.)
(Remember the Recipients users must use the blank ,if you use ',' ,the email cann't send always)
22) Build Now-->You will receive a email(If you build success.you will not receive a email.Please let you code error,you will receive a email).