Maven Learning Summary (ii)--maven project building process Exercises

Source: Internet
Author: User

Maven Learning Summary (ii)--maven project building process Exercises

The previous article was just a brief introduction to MAVEN's knowledge of getting started, this article is about the process of building a project with MAVEN's highly automated

I. Create a MAVEN project 1.1, build a Hello Project

1. First build the Hello Project, and set up the directory structure and pom.xml file of MAVEN contract.

Hello
| --src
| -----Main
| ----------Java
| ----------Resources
| -----Test
| ---------Java
| ---------Resources
| --pom.xml

  

2. Edit the Pom.xml in the project Hello root directory and add the following code:

1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"  2 xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > 3   <modelVersion>4.0.0</modelVersion> 4   <groupId>me.gacl.maven</groupId> 5   <artifactId>Hello</artifactId> 6   <version>0.0.1-SNAPSHOT</version> 7   <name >Hello</name> 8    9     <!--add dependent jar package-->10     <dependencies>11         <!-- The project is going to use the jar package to JUnit, so add a dependency-->12 <dependency>13 for the JUnit jar package here             <groupid>junit</groupid >14             <artifactid>junit</artifactid>15             <version>4.9</version>16             < scope>test</scope>17         </dependency>     </dependencies>20 </ Project>

3. Create a new file in the Src/main/java/me/gacl/maven directory Hello.java

  

The code for Hello.java is as follows:

1 Package me.gacl.maven;2 3 public class Hello {4     5 public     String SayHello (String name) {6         return "Hello" +name +"!"; 7     }8}

4. Create a new test file in the/src/test/java/me/gacl/maven directory Hellotest.java

  

The code for Hellotest.java is as follows:

1 package Me.gacl.maven; 2//Import JUnit Package 3 import org.junit.Test; 4 import static junit.framework.assert.*; 5  6 public class Hellotest {7  8     @Test 9 public     void Testhello () {ten         hello hello = new Hello (); 11
   string results = Hello.sayhello ("GaCl");         assertequals ("Hello gacl!", results);     }14}
1.2. Compile, clean, test, and package your project with Maven

1. Compile the project using MAVEN, the command to compile the project is: "mvn compile"

Open the cmd command line,

  

Enter the Java class for the "mvn compile" command to compile the project in the Hello Project root directory

  

After compiling successfully, you can see a "target" folder in the root directory of the Hello Project, which is the folder that Maven helped us build after the compilation was successful, as shown in:

  

Open the "target" folder and you will see a "classes" folder in it, as shown in:

  

The "Classes" folder contains Maven's compiled Java class, as shown in:

  

This is the process of automatically compiling projects using MAVEN.

2. Using Maven cleanup project, the command to clean up the project is: "mvn"

  Go to the Hello project root to execute the "mvnClean" command cleanup project, the process of cleaning up the project is to execute the "mvn compile" command to compile the project generated when the target folder is deleted, as shown in:

  

3. Using the MAVEN test project, the command to test the project is: "mvnTest"

Go to the Hello Project root directory to execute the "mvnTest" command, as shown in the project:

  

After the test is successful, you can see a "target" folder in the root directory of the Hello Project, which is the folder that Maven helped us build after the test was successful, as shown in:

  

Open the "target" folder, you can see there is a "classes" and "test-classes" folder, as shown in:

  

That is, when we execute the "mvnTest" command to test the project, MAVEN helps us compile the project before executing the test code.

4. Using MAVEN to package the project, the command for packaging the project is: "MVN package"

  Go to the Hello Project root directory to execute the "mvn Package" command to test the project as shown in:

  

  

After the package is successful, you can see the "target" in the root directory of the Hello Project One more Hello-0.0.1-snapshot.jar in the folder, this Hello-0.0.1-snapshot.jar is the jar file that Maven helped us build after the package was successful, as shown in:

  

5 , using the MAVEN deployment project, the command to deploy the project is: "MVN install"

  Go to the Hello Project root directory and execute the "mvn Install" command to test the project as shown in:

  

  

After the installation succeeds, the "target" folder is generated at the root of the Hello project and the "target" folder is opened. You can see that there will be Hello-0.0.1-snapshot.jar, this hello-0.0.1-snapshot.jar is the jar file that Maven helped us build after the installation was successful, as shown in:

  

In addition, there will be a hello-0.0.1-snapshot.jar in the warehouse where we store the jar packages that Maven downloads, so the process of MAVEN setup is actually to "clean up" → "compile" → "Test" → "pack" Then put the packaged jar into the MAVEN repository we specified for the jar package, as shown in:

  

So using the "mvn Install" command, the process of "clean up" → "compile" → "test" → "package" of the Maven build project is done, and the packaged Jar package is published to the local MAVEN repository, so MAVEN's most commonly used command is "MVN Install ", this command can do the most things.

1.2. Combine the commands with Maven

MAVEN's compilation, cleanup, testing, packaging, deployment commands can be combined with several commands at the same time, common command combinations are as follows:

1, first clean up and then compile: "mvn-Compile", as follows:

  

There are also "mvn clean test", "MVN Clean package", "mvn clean install", these combination commands are more commonly used.

The above is a demonstration of each process of the MAVEN build project.

Second, use the jar package for projects generated through MAVEN installation in other projects

Above, we use the MVN install command to package the Hello project into a Hello-0.0.1-snapshot.jar package and publish it to a local maven repository E:\repository\me\gacl\maven\Hello\ 0.0.1-snapshot, let's take a look at how to use Hello-0.0.1-snapshot.jar in other projects

1. Create a new Hellofriend project while creating a directory structure and pom.xml file for MAVEN conventions
Hellofriend
| --src
| -----Main
| ----------Java
| ----------Resources
| -----Test
| ---------Java
| ---------Resources
| --pom.xml

As shown in the following:

  

2, edit the project hellofriend root directory Pom.xml, add the following code:

 1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 <groupId>me.gacl.maven</groupId> 5 <artifactId> Hellofriend</artifactid> 6 <version>0.0.1-SNAPSHOT</version> 7 <name>hellofriend</name > 8 9 <!--add dependent jar package--&GT;10 <dependencies>11 <!--project to use the jar package to JUnit, so here is the dependency of the JUnit jar package added-- >12 <dependency>13 <groupid>junit</groupid>14 <artifactid>junit         </artifactid>15 <version>4.9</version>16 <scope>test</scope>17             </dependency>18 <!--project to use the jar package to Hello, so add the dependency-->19 of the Hello jar package here <dependency>20     <groupid>me.gacl.maven</groupid>21        <artifactid>hello</artifactid>22 <version>0.0.1-snapshot</version>23 <scope>compile</scope>24 </dependency> </dependencies>26 </project>

3. Create a new file Hellofriend.java in the Src/main/java/me/gacl/maven directory, as shown in:

  

The code for Hellofriend.java is as follows:

1 package Me.gacl.maven; 2  3 import Me.gacl.maven.Hello; 4  5 public class Hellofriend {6  7 public     string Sayhellotofriend (string Name) {8          9         Hello hello = new Hello (), ten         String str = Hello.sayhello (name) + "I AM" +this.getmyname ();         Stem.out.println (str);         return str;13     }14 public     String getmyname () {         "John";     }18}

4. Create a new test file Hellofriendtest.java in the/src/test/java/me/gacl/maven directory, as shown in:

  

The code for Hellofriendtest.java is as follows:

1 package Me.gacl.maven; 2  3 import static junit.framework.Assert.assertEquals; 4 import org.junit.Test; 5 import Me.gacl.maven.Hello; 6  7 public class Hellofriendtest {8  9     @Test10 public     void Teshellofriend () {         hellofriend Hellofriend = new Hellofriend (),         String results = hellofriend.sayhellotofriend ("GaCl"), and         assertequals (" Hello gacl! I am John ", results);     }16}

5, in the Hellofriend directory to execute the command "MVN package" Test Hello-0.0.1-snapshot.jar inside the class whether the reference succeeds, as follows:

  

Maven Learning Summary (ii)--maven project building process Exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.