First, the preparation of POM
<groupId>Com.shine.myapp</groupId><Artifactid>Hello-world</Artifactid><version>0.0.1-snapshot</version><Packaging>Jar</Packaging>
- GroupId: Defines which group the project belongs to.
- Artifactid: Defines the unique ID of the current MAVEN project in the group.
- Version: Defines the current version of the project.
Second, write the main code
- 1, write the main code Helloword.
- 2, execute mvn clean compile, execution sequence:clean:clean,resources:resources,compile:compile.
- Clean tells Maven to clear the output directory target/.
- Compile tells Maven to compile the project Master code.
Third, write the test code
1. Add a pom dependent junit:
<Dependencies> <Dependency> <groupId>Junit</groupId> <Artifactid>Junit</Artifactid> <version>4.7</version> <Scope>Test</Scope> </Dependency></Dependencies>
- dependencies element: The element under change can contain multiple dependency elements to affirm the dependencies of the project.
- GroupId, Artifactid, Version:maven can automatically download Unit-4.7.jar in the warehouse.
- Scope: To declare the scope, if the dependency is test, the dependent corresponding tests are valid; The default value is compile, which indicates that the dependency is valid for both the main code and the test code.
2, write unit test code.
3, execute mvn Clean test, execution order: Clean:clean, Resources:resources, Compile:compile, Resources:testresources, Compile:testcompile.
4, Configuration Maven-compile-plugin support Java6:
<plugin> <groupId>Org.apache.maven.plugins</groupId> <Artifactid>Maven-compiler-plugin</Artifactid> <Configuration> <Source>1.6</Source> <Target>1.6</Target> </Configuration></plugin>
Iv. Packing and running
1. Packing: MVN clean Package
2. Installation: mvn clean install
3, hit the jar executable main function, add Maven-shade-plugin plug-in:
<plugin> <groupId>Org.apache.maven.plugins</groupId> <Artifactid>Maven-shade-plugin</Artifactid> <version>1.2.1</version> <executions> <Execution> <Phase>Package</Phase> <Goals> <goal>Shade</goal> </Goals> <Configuration> <Transformers> <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <MainClass>Com.shine.helloworld.HelloWorld</MainClass> </Transformer> </Transformers> </Configuration> </Execution> </executions></plugin>
Run MVN Clean Package , There are two Jar:hello-world-0.0.1-snapshot.jar, Original-hello-world-0.0.1-snapshot.jar, the former is an executable jar And the latter is the original Jar.
Summarize:
MVN clean compile, mvn clean test, MVN clean package, mvn clean install,
MVN clean test will execute mvn clean compile;
MVN Clean Package will execute MVN clean test;
MVN clean install will execute MVN clean package.
The "Maven practical" note, "Iii. Introduction to MAVEN's use"