在安裝好Maven後,編寫一個簡單的不依賴IDE的Hello World項目。
3.1 編寫pom文件
<?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.liuxm.test</groupId>
<artifactId>hello-word</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
</project>
modelVersion指定了當前pom的版本。3.2 編寫java程序代碼
package com.liuxm.test;
public class HelloWorld{
public String sayHello(){
return "Hello World";
}
public static void main(String[] args){
System.out.println(new HelloWorld().sayHello());
}
}
注意:然後執行resource任務,由於沒有定義此項,所以跳過了。
3.3 編寫測試代碼
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="java"><?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.liuxm.test</groupId>
<artifactId>hello-word</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.7
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
代碼添加dependencies元素,該元素下可以包含多個dependency元素聲明項目的依賴。這裏添加了依賴,groupId ,artifactId,version是4.7.
package com.liuxm.junit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.liuxm.test.HelloWorld;
public class HelloWorldTest
{
@Test
public void testSayHello()
{
HelloWorld helloworld = new HelloWorld();
String result = helloworld.sayHello();
assertEquals("Hello World",result);
}
}
在主目錄下:運行mvn clean test命令。3.4 打包與運行
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.liuxm.test.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
然後在運行:mvn clean install 項目在打包時會將mainclass信息放到manifest.mf文件裏面,打開target可以看到裏面有hello-word-1.0-SNAPSHOT.jarManifest-Version: 1.0
Build-Jdk: 1.6.0_21
Built-By: Administrator
Created-By: Apache Maven 3.2.1
Main-Class: com.liuxm.test.HelloWorld
Archiver-Version: Plexus Archiver
文件中已經包含了Main-Class: com.liuxm.test.HelloWorld
接下來在根目錄下執行這個jar文件: