Maven (i) How to create a maven project with eclipse

Source: Internet
Author: User
Tags mysql in object model xmlns
1. What is Maven


Apache Maven is a project management and integration tool. Based on the concept of engineering object Model (POM), through a central information management module, Maven is able to manage project builds, reports, and documentation.



The MAVEN engineering structure and content is defined in an XML file--pom.xml, which is the short name of the project Object Model (POM), which is the underlying component of the entire MAVEN system. 


2. Why use maven



When we create a MAVEN project, we do not need to import the various jar packages ourselves, the jar package dependency information that will be imported is configured in Pom.xml, and the MAVEN project will automatically import the appropriate dependencies from the local repository when compiling, testing, and running according to the specific configuration.



There are two of the biggest benefits, one is that project dependencies are managed uniformly, not prone to dependency conflicts (such as the version conflict introduced by the jar package), and the other is that the size of the project is smaller, after all, a slightly larger project simply introduces a jar package that takes up a considerable amount of volume, And the MAVEN project just needs a pom.xml file to get it done.



conventions better than configuration



Maven uses conventions rather than configurations, and developers don't need to care about every configuration detail. When you create a MAVEN project, MAVEN creates the default engineering structure. Developers only need to properly place files and add dependencies to the project in Pom.xml.



The MAVEN project specifies the default configuration for source files, resource files, test files, output files, and so on, simply to put these file rules in the corresponding path for easy management, as shown in the following table, assuming that ${basedir} represents the project root directory:


Path to Project engineering types of files stored
${basedir}/src/main/java Project Source
${basedir}/src/main/resources Resource files, such as. properties files
${basedir}/src/main/webapp Web project-related resource files, such as XML
${basedir}/src/test/java The source code used for testing
${basedir}/src/test/resources Resource files for testing
${basedir}/target/classes Post-compilation files
${basedir}/target/test-classes Files that are used for testing after compilation



If it is a MAVEN project built with Eclipse's Maven plugin, it will only automatically generate Src/main/resources and target/classes, target/test-classes, and need to manually add additional directories. and specify the source of the project and the installation and setting environment variables of the compiled output directory 3.Maven



to create a MAVEN project, you need to install MAVEN and set up environment variables . MAVEN Download Settings Environment variables
Create a new variable maven_home, a directory with a value of Maven X:\XXX\apache-maven-XXX add%maven_home%\bin to the path variable to run cmd, enter MVN- After v you can see the version information of Maven and so on, which means the installation was successful 


4. There are two ways to create a Maven project and a dependency 4.1 to create a MAVEN project, as follows



first method of creation: Manually created using the command line


MVN archetype:generate-dgroupid=com.lewis.seckill-dartifactid=seckill-dpackage=com.lewis.seckill-dversion=1.0- Snapshot-darchetypeartifactid=maven-archetype-we

 


A Maven-archetype-webapp skeleton Maven project is created after the command line executes, where GroupID is the unique identifier of the project organization, which actually corresponds to the structure of the Java package; Artifactid is the unique identifier of the project, the name of the actual corresponding project The package is generally groupid+artifactid, is automatically generated and can be modified



second way to create: Create a project with the Maven plugin from the IDE tools



The author uses eclipse, so here's just how Eclipse uses plug-ins to build a MAVEN project



Eclipse Install Maven plugin do not know how Maven plug-in, please refer to this blog post, recommended to use the link method of manual installation
If the Maven plugin is manually installed, there may be a lack of pom.xml graphical editing tools, please add additional, please refer to the blog post has installed the Maven plugin please go to the next step



File→new→other ... →maven Project→next, enter the following interface






Click Next to select the skeleton Maven-archetype-webapp you want to build, as shown below






Click Next, fill in the Groupid=com.lewis.seckill,dartifactid=seckill,package=com.lewis.seckill (fill in according to the actual situation), then finish



turn MAVEN projects into eclipse-supported Web projects



This is a MAVEN project that needs to be transformed into an eclipse-supported Web project so that it can be deployed directly to Tomcat for debugging on the eclipse. For details, please refer to this blog post 4.2 To modify the Pom.xml file



When the MAVEN project is created, there is a pom.xml file under the root directory, and the MAVEN project manages the project dependencies through Pom.xml, and without the XML file, eclipse will not treat the project as a MAVEN project



adding a project requires a jar package dependency


<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.lewis</groupId>
    <artifactId> seckill</artifactid>
    <packaging>war</packaging>
    <version>0.0.1-snapshot</ version>
    <name>seckill Maven webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactid >junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


In this pom.xml, junit dependencies are added automatically, but it is possible that the 3.X version will be changed to 4.11 because the previous version of JUnit does not support annotation testing.



The project needs to rely on, we go to the file to add the corresponding configuration information, such as we want to use MySQL, in this pom.xml file dependencies tag to introduce the following configuration


        <dependency>
            <groupId>mysql</groupId>
            <artifactid>mysql-connector-java</ artifactid>
            <version>5.1.35</version>
            <scope>runtime</scope>
        </ Dependency>


Each time the Pom.xml file is updated and saved, the Maven Project automatically detects the download of the new dependency. The Groupid+artifactid here is the only driver jar package that locates MySQL in the repository, and version indicates its release, and scope represents the range of the dependency. In a MAVEN project, compiling, testing, and running using a different classpath,runtime means that the project does not import the dependency at compile time, but imports it when testing and running the project.



So how do you find the jar package coordinates and version information? Please refer to this blog post



issues that may exist when updating dependencies



As MAVEN defaults to the central repository download dependency, the download speed may be very slow, or even not connected, generally we will set up the image of Alibaba cloud, download speed will be much faster, please refer to the blog post



run the maven command on Eclipse



After installing the Maven plugin, you do not need to use CMD to enter the command, you can directly enter the directive in Eclipse. In the cmd each command to add MVN prefix, and in eclipse do not need, the general commonly used directives have clean, compile, install, the package, etc., generally in the execution of the command before the run to clear the compiled file



Right-click on the MAVEN project, execute the command in run as, or enter other commands yourself, please refer to this blog post for details.



issues that may occur when you perform a maven command



If you are creating a MAVEN project on eclipse for the first time, there may be an input maven command, but the Eclipse console is unresponsive, because there are no arguments, please refer to this blog post for details.



To this step, our MAVEN project has been initially established, the next only need to install the rules under the Src/main/java to place the source code, under the src/main/resources to put the resource files on the line



RELATED LINKS There are dependencies and dependency transitivity that you want to know about Maven you can refer to this blog post for a Maven project on how to create a Web skeleton in eclipse


Related Article

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.