Task:

After installing Maven, create the Maven version HelloWorld.

(Install maven Mac: $ brew install maven)

Maven project directory would be like:

1
2
3
4
5
6
7
8
src
-main
-java
-package
-test
-java
-package
resources

  1. Create an empty folder codeMaven to store all maven projects.
  2. Under codeMaven, create folder maven01.
  3. under maven01, create src.

src would be where source code would be created.

Under src, create 2 folders, main, test.

main

In main, create java.

java would be where java file would be stored.

  1. In java, create com.
  2. create imooc under com
  3. create maven01 under imooc.
  4. create model under maven01.

package name is com.imooc.maven01, module name is model.

Create java file HelloWorld.java in module model:

1
2
3
4
5
6
7
package com.imooc.maven01.model;

public class HelloWorld {
public String sayHello() {
return "Hello World Maven!";
}
}

test

In test, create exactly the same folder structure. Create a java file HelloWorldTest.java under module model:

1
2
3
4
5
6
7
8
9
10
11
package com.imooc.maven01.model;

import org.junit.*;
import org.junit.Assert.*;

public class HelloWorldTest {
@Test
public void testHello() {
Assert.assertEquals("Hello World Maven!", new HelloWorld().sayHello());
}
}

pom.xml

Now the src folder is completed. A pom.xml is needed to manage this source folder. Get the structure of pom.xml from struts2 core ... pom.xml at this link, delete all dependencies and just keep <?xml>, <project> and <modelVersion>:

1
2
3
4
5
6
7
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


</project>

Add maven coordinates and junit dependency(used in test):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- groupId: project package name -->
<!-- artifactId: module name -->
<groupId>com.imooc.maven01</groupId>
<artifactId>maven01-model</artifactId>
<version>0.0.1SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>

The structure under maven01 is now like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pom.xml
src
-main
-java
-com
-imooc
-maven01
-model
HelloWorld.java
-test
-java
-com
-imooc
-maven01
-model
HelloWorldTest.java

run

Now, under top directory maven01, run mvn compile. This compile the whole maven project. Should be something similar to this:

1
2
3
4
5
6
7
8
9
10
11
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-model ---
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-model ---

...

[INFO] Compiling 1 source file to /Users/xialei/Desktop/codeMaven/maven01/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.819 s

BUILD SUCCESS.

Run test: mvn test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile)
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01-model ---
...
ownloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 kB at 2.1 MB/s)

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.imooc.maven01.model.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.022 sec

Results :

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

This shows tests passed.

root directory change

1
2
3
pom.xml
src
target

A new directory target is generated. Under this folder are

  1. classes folder where compiled class files stored,
  2. surefire-reports;
  3. test-classes,
  4. etc.

extra

mvn package: generate maven01-model-0.0.1SNAPSHOT.jar under target. Generate a SNAPSHOT.

I’ve put this maven01 in my Github. Take a look at it should any problem arise.

Extra question: how I push this petite project to github?

Take a look at this link

If an error of ! [rejected] master -> master (fetch first) shows up, go to this StackOverFlow

本来想写个”以上”的,跟”钦此”类似,现在不流行了。