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
8src
-main
-java
-package
-test
-java
-package
resources
- Create an empty folder
codeMavento store all maven projects. - Under
codeMaven, create foldermaven01. - under
maven01, createsrc.
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.
- In
java, createcom. - create
imoocundercom - create
maven01underimooc. - create
modelundermaven01.
package name is com.imooc.maven01, module name is model.
Create java file HelloWorld.java in module model:1
2
3
4
5
6
7package 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 | package com.imooc.maven01.model; |
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
16pom.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 | pom.xml |
A new directory target is generated. Under this folder are
classesfolder where compiled class files stored,surefire-reports;test-classes,- 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
本来想写个”以上”的,跟”钦此”类似,现在不流行了。