Getting Started with Maven (including example tutorials)

Original address: http://blog.csdn.net/u013142781/article/details/50316383

Maven, a project management and build automation tool, is used by more and more developers to manage jar packages in projects. Next, Xiaobaoge will introduce maven from the following directions:

(1) Brief introduction to Maven
(2) Maven installation and configuration
(3) Maven to build projects with eclipse
(4) Common commands of
Maven (5) Maven examples

The door is tight, and it takes off immediately:

(1) A brief introduction to

Maven Maven, a project management and build automation tool, is used by more and more developers to manage jar packages in projects. But for us programmers, what we care most about is its project-building capabilities. So here we introduce how to use maven to meet the daily needs of our project. Maven uses the principle of convention over configuration. It requires that all projects have the following structure before customization:

write picture description here

A maven project will generate JAR files by default, in addition, the compiled classes will be placed under basedir/target/classes, the JAR file will be placed under ${basedir}/target. At this point someone will say, Ant does not have so many requirements, it allows you to freely define the structure of the project. I don't want to cause a war of words here, I personally feel that these default definitions of maven are very convenient to use. Ok, let's install maven next.

(2) Maven installation and configuration

2.1. Download the maven installation package: apache-maven-3.2.1 http://download.csdn.net/detail/u013142781/9355367, decompress it and use it.

2.2, maven environment variable configuration, the configuration method is somewhat similar to jdk. Create a new environment variable MAVEN_HOME (the value is the root directory of maven), and then add %MAVEN_HOME%\bin; to the PATH environment variable.

2.3. Set the setting.xml file to configure the local warehouse. There is a setting.xml file under apache-maven-3.2.1\conf, open it, and set the value of localRepository to the path of your local repository. (The blogger created a repository folder as a local repository in the apache-maven-3.2.1 root directory). As shown below, write a picture description

here

2.4, configure maven to eclipse, download the plugin eclipse-maven3-plugin http://download.csdn.net/detail/u013142781/9355661, decompress the corresponding features and plugins two folders Copy the contents of eclipse to the same folder of eclipse, then restart eclipse, when you open Window→Preferences..., you should be able to see a Maven option in an option list:

write image description here

Then , Windows->Prefrences –>Installations–>Add. Select the root directory of maven for installation name, and then press OK all the time.

write picture description here

At this point, the maven installation configuration is complete! If you need to understand the environment construction of jdk+eclipse+svn+maven+mysql+tomcat7.0, you can read Xiaobaoge's blog http://blog.csdn.net/u013142781/article/details/50300233, which is very detailed and also provides Install the package.

(3) Maven joins hands with eclipse to build the project. For the

specific creation method, you can check a blog of my friend: Spring+Mybatis+SpringMVC+Maven+MySql build example, http://blog.csdn.net/evankaka/article/details/48785513 , very very detailed. Of course, Xiao Baoge will also write a "Spring+Mybatis+SpringMVC+Maven+MySql Construction Example" in the future, I hope to pay more attention to support.

(4) Common commands of

Maven Common commands of Maven:
1. Create a common java project of Maven:
mvn archetype:create -DgroupId=packageName -DartifactId=projectName
2. Create a web project of Maven:
mvn archetype:create -DgroupId=packageName -DartifactId =webappName-DarchetypeArtifactId=maven-archetype-webapp
3. Compile the source code: mvn compile
4. Compile the test code: mvn test-compile
5. Run the test: mvn test
6. Generate site: mvn site
7. Package: mvn package
8. Install jar in the local Repository: mvn install
9. Clean the generated project: mvn clean
10. Generate eclipse project: mvn eclipse:eclipse
11. Generate idea project: mvn idea:idea
12. Use the goal command in combination, such as only package without testing: mvn -Dtest package
13. Compile the test content: mvn test-compile
14. Only jar package: mvn jar:jar
15. Only test without compiling, Do not test compilation: mvn test -skipping compile -skipping test-compile
(flexible use of -skipping, of course, can also be used for other combined commands)
16. Clear some system settings of eclipse: mvn eclipse:clean

(5) Maven instance

1 , Maven project creation Write the picture description

here

2. Select the quick framework and write the picture description

here

3. Output the project name, package (Packaging, if it is just an ordinary project, just choose jar, if it is a web project, choose war, here we choose jar)

Write the picture description here

4. The created directory is as follows:

Write the picture description here

5. The pom.xml at the beginning is like this. Write

the picture description here.

6. Now we add the dependency of the mysql driver package and edit the pom.xml as follows:
<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">project</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xmlns</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://maven.apache.org/POM/4.0.0"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xmlns:xsi</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xsi:schemaLocation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>></span>">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xsi:schemaLocation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>></span>">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xsi:schemaLocation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>></span></span>></span></span>></span>
  <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">modelVersion</span>></span>4.0.0<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">modelVersion</span>></span>
  <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">groupId</span>></span>com.luo<span class="hljs-tag" style="color:rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">groupId</span>></span>
  <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">artifactId</span>></span>first_maven_project<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">artifactId</span>></span>
  <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">version</span>></span>0.0.1-SNAPSHOT<span class="hljs-tag"style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">version</span>></span>

  <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dependencies</span>></span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"><!-- mysql驱动包 --></span>
        <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dependency</span>></span>
            <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">groupId</span>></span>mysql<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">groupId</span>></span>
            <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">artifactId</span>></span>mysql-connector-java<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">artifactId</span>></span>
            <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">version</span>></span>5.1.29<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">version</span>></span>
        <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dependency</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dependencies</span>></span>

<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">project</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing:border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

7. Edit pom.xml as above, and then save it, then the package will be automatically downloaded from the central warehouse, and you will find that there is one more package

here: write a picture description here,

and then you can use this package normally

8. Other packages you want to depend on, The corresponding dependency can be found on this website:
http://mvnrepository.com/

Write a picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260065&siteId=291194637