Compile pom:The core of the maven project is Pom. xml. Pom (Project object model) defines the basic information of a project, describes how a project is built, declares project dependencies, and so on. Write a simple Pom. xml file for the hello World project.
<?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.juvenxu.mvnbook</groupId> <artifactId>hello-world</artifactId> <version>1.0-SNAPSHOT</version> <name>Maven Hello World Project</name></project>
The first line of the Code is the xml header, which specifies the version and encoding method of the XML document. Followed by the project element, the project is all pom. XML root element, which also declares some pom-related namespaces and XSD elements, although these attributes are not mandatory, however, using these attributes allows third-party tools to provide the casual functionality of the XML. Modelversion specifies the current POM model version. For maven2 and maven3, it can only be 4.0.0. The groupid, artifactid, and version elements define the basic coordinates of a project. Groupid defines the group to which the project belongs. This group is often associated with the organization or company of the project. For example, if you create a group named Myphone on googlecode, The groupid is com. googlecode. Myphone. Artifactid defines the unique ID of the current Maven project in the group. It is usually the name of a project or subitem. For example, if a project in the Myphone group is Google-phone, you may allocate artifactid to different sub-projects (modules, such as Google-phone-util, Google-phone-domain, and Google-phone-web. Version specifies the current version 1.0-snapshot of the Project. snapshot indicates a snapshot, indicating that the snapshot is still under development and is an unstable version. The version is constantly upgraded, such as 1.0 and 1.1-snapshot.
Write the main code:The project main code is packaged into the final component (such as jar), and the test code is only used when the test is run. By default, the main code of the project is located in the src/main/Java directory. The advantage of creating the main code in this directory is that no additional configuration is required. In the future use process, maven automatically searches for the directory to find the main project code. We compile helloworld. the directory where Java is located is src/main/Java/COM/juvenxu/mvnbook/helloworld. java, the Java file package name is: COM. juvenxu. mvnbook. helloworld, which is consistent with the gruopid and artifactid defined in POM. Generally, the Java class package names in the project should be based on the groupid and artifactid of the project, which is clearer and more logical, it is also convenient to search for components.
package com.juvenxu.mvnbook.helloworld;public class HelloWorld{ public String sayHello() { return "Hello Maven"; } public static void main(String[] args) { System.out.print(new HelloWorld().sayHello()); }}
After the code is compiled, use Maven for compilation. Run the command MVN clean compile in the project root directory and the following output is displayed:
Three plug-ins are executed, Maven-clean-plugin, Maven-resources-plugin, and Maven-compile-plugin. Clean clears the output directory target /, resouce will process resource files, compile will compile the main project code and put it in target/classes (the compiled class is com/juvenxu/mvnbook/helloworld. class ). As you can see, Maven can easily execute project cleanup and compilation tasks.
Compile the test code:To keep the project structure clear, the main code and test code should be in separate directories. The default test code directory in the maven project is src/test/Java. This directory should be created before writing test cases. To use JUnit for unit testing, we need to add the dependency on JUnit components in POM. xml. The modified Pom. XML is as follows:
<?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.juvenxu.mvnbook</groupId> <artifactId>hello-world</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></project>
The dependencies element is added to the Code. This element can contain multiple dependency elements to declare project dependencies. A coordinate of JUnit is added here. With this statement, MAVEN will automatically access the central repository http://search.maven.org/#browseto download the required files. Above pom. in the XML code, there is also an element scope with the value of test. scope is the dependent range. If the dependent range is test, it indicates that the dependency is only valid for the test, this JUnit will cause errors in the main code, but it will not cause errors in the test code. The default value of scope is compile, indicating that the dependency is valid for both the main code and the test code. Next, write the test code:
package com.juvenxu.mvnbook.helloworld;import static org.junit.Assert.assertEquals;import org.junit.Test;public class HelloWorldTest{ @Test public void testSayHello() { HelloWorld helloWorld = new HelloWorld(); String result = helloWorld.sayHello(); assertEquals("Hello Maven",result); }}
After you write a test case, you can call Maven to perform the test. Run MVN clean test. (Note: In a newer version of Maven, the problem that the annotation of JUnit cannot be solved is not prompted, for example, @ test ). Although only MVN clean test is input in the command line, the actual processing done by Maven includes: Clean (the Clean target for executing Maven-clean-plugin, which is similar to the following), resources: resources, compile: compile, resources: testrescources, compile: testcompile, surefire: test. You need to know that before Maven executes the test (surefire: test, this is a feature of the maven life cycle. After the test code is compiled, a test file is generated under target/test-classes. Before executing the appeal processing, Maven reads Pom. xml and performs some operations, such as checking whether the dependency exists in the local repository. If the dependency does not exist, download it from the central repository. In the preceding example, Maven downloads the junit-4.7.pom and junit-4.7.jar files.
Package and run:The package type is not specified in the hello-world Pom. The default package type jar is used. Run the MVN clean package command to package the package. Maven will execute compilation, testing, and other operations before packaging, the packaged file is located in target/, it is named according to the artifact-version.jar rules. To allow other Maven projects to directly use this jar package, you also need to execute an installation command, MVN clean install, and install it in the local repository. Open the corresponding directory of the local repository and you can see the POM and jar of the hello-World project. We have learned the main Commands of Maven: MVN clean compile, MVN clean test, MVN clean package, and MVN clean install. Run clean before executing compile, run compile before executing test, run test before executing package, and run package before executing install. There is a main method in our helloworld class, but the jar package generated by default cannot be run directly. We need to add the main method in pom. configure the maven-shade-plugin plug-in XML to implement this function.
<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> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.juvenxu.mavenbook.helloworld.HelloWorld</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
The relative position of the plugin element in POM should be under <project> <build> <plugins>. Then run the MVN clean package again for packaging. Now, run the JAR file in the project root directory:
We can see that the main function is successfully executed.
Use archetype to generate the Project Skeleton:Maven has some conventions: Place POM in the root directory of the project. XML, put the project's main code in the src/main/Java directory, and put the project's test code in src/test/Java .... if we create these folders manually every time, it will be too difficult. To this end, Maven provides archetype to help us quickly outline the Project Skeleton. Enter the MVN archetype: Generate command in your workspace directory. For example:
Maven requires you to select the archetype type. Generally, the default Maven-Archetype-Quickstart type is used. You do not need to enter the Enter key, and then select this type of version, we recommend Maven-Archetype-Quickstart to use 1.1. You must enter some configuration information of the project, such as groupid, artifactid, version, and package. A basic Archetype is used here. If many projects have similar custom project structures and configuration files, you can define your own archetype, use your archetype to generate a skeleton in future projects.
M2eclipse:
1. Import Maven ProjectIn actual work, we almost cannot do without ide. Here we will first explain how to import Maven projects from eclipse, provided that the m2eclipse plug-in has been installed. Eclipse-> file-> Import-> Maven-> existing Maven project, click Next, and select the root directory of the maven project. You can see:
Click Finish. After the import is complete, the project structure shown in is displayed in the package explorer view.
2. Create a Maven ProjectFile-> New-> Project-> Maven project, click Next, use the default option in the pop-up dialog box, and click Next again (do not select create a simple project, so that we can use archetype to build the skeleton). In the dialog box that appears, select archetype, select Maven-Archetype-quicksort, and click Next. A new dialog box is displayed, asking us to enter information such as groupid, artifactid, version, and package. After entering the information, click Finish to create the maven project.
3. Run the maven commandUsing m2eclipse, You can execute Maven commands in eclipse, and view the build output in the eclipse console. Right-click the maven project or Pom. XML, and select run as from the shortcut menu to view common Maven commands. If you do not find the desired command, you can also select Maven build... from the common Maven command menu ..., in the displayed dialog box, enter the maven command you want to execute in the goal column. Note that the command should not contain the MVN prefix.
Maven learning notes (3): getting started with Maven