`

用 Maven 编译最简单的 java 程序

 
阅读更多

参考地址: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

下载 Maven 地址:http://maven.apache.org/download.cgi#Installation

设置 Maven 的环境变量: 变量名:M2_HOME   变量值: C:\apache-maven-3.0.5

设置 Path 的环境变量:C:\apache-maven-3.0.5\bin;

建立maven的目录结构,我在这个基础上新增了A.java B.java 和相应的测试类

my_app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- xjh
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- xjh
                    `-- app
                        `-- AppTest.java

App.java
package com.xjh.app;

public class App {
	
	public int add(int a, int b) {
		return a + b;
	}
	
}


AppTest.java
package com.xjh.app;

import org.junit.Test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class AppTest extends TestCase {
	
	public void testAdd() {
		App app = new App();
		Assert.assertEquals(3, app.add(1, 2));
		Assert.assertEquals(2, app.add(1, 1));
	}	
	
}


A.java

package com.xjh.app;

public class A {
	
	public int sum(int a, int b) {
		return a + b;
	}
	
}


B.java

package com.xjh.app;

public class B {
	
	public int sub(int a, int b) {
		return a - b;
	}
	
}


ATest.java

package com.xjh.app;

import org.junit.Test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class ATest extends TestCase {
	
	public void testSum() {
		A a = new A();
		Assert.assertEquals(3, a.sum(1, 2));
		Assert.assertEquals(2, a.sum(1, 1));
	}
	
}


BTest.java

package com.xjh.app;

import org.junit.Test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class BTest extends TestCase {
	
	public void testSub() {
		
		B b = new B();
		Assert.assertEquals(4, b.sub(9, 5));
		Assert.assertEquals(-1, b.sub(5, 6));
		
	}
	
}


AllTest.java

package com.xjh.app;

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTest {
	
	public static Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTestSuite(ATest.class);
		suite.addTestSuite(BTest.class);
		suite.addTestSuite(AppTest.class);
		
		return suite;
		
	}
	
}


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.xjh.app</groupId>
  <artifactId>my_app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


在控制台运行

D:\maven_test\my_app>mvn package

运行结果:

D:\maven_test\my_app>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ my_app ---

[debug] execute contextualize
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\maven_test\my_app\src\main\resourc
es
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ my_app ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 3 source files to D:\maven_test\my_app\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ my
_app ---
[debug] execute contextualize
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\maven_test\my_app\src\test\resourc
es
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ my_ap
p ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 4 source files to D:\maven_test\my_app\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ my_app ---
[INFO] Surefire report directory: D:\maven_test\my_app\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xjh.app.AllTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec
Running com.xjh.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
Running com.xjh.app.ATest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Running com.xjh.app.BTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec

Results :

Tests run: 6, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ my_app ---
[INFO] Building jar: D:\maven_test\my_app\target\my_app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.973s
[INFO] Finished at: Fri Mar 08 21:44:35 CST 2013
[INFO] Final Memory: 11M/28M
[INFO] ------------------------------------------------------------------------
D:\maven_test\my_app>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics