Maven tutorial (4) - New Maven Project

Original Address: https://blog.csdn.net/liupeifeng3514/article/details/79542203

Our simple helloworld entry as an example, some people say you have mastered the helloworld mastered half of this technology, and for maven, you grasp helloworld, you may also sleepwalk.

1. Create a template project from maven

At the command prompt (Windows), browse to the Java project you want to create a folder. Type the following command:

mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

project-packaging: Project Package name
project-name: Project Name

This tells Maven to create a Java project from maven-archetype-quickstart templates. If you ignore archetypeArtifactId option, a huge list of templates Maven will be listed.

For example, where the working directory is D: \ workspace_maven, execute the command procedure time may be relatively long, depends on the individual network conditions.

 

 In the above case, a new Java project named "HelloWorld", and the directory structure of the entire project will be created automatically.

Note: There are a few people say mvn archetype: generate command failed to generate the project structure. If you have any similar problems, do not worry, just skip this step, manually create the folder.

I use the command:

mvn archetype:generate -DgroupId=com.lpf.mvn -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2, Maven project directory layout

  • src / main / java: used to store source code
  • src / main / resources: the source code used to store resource files
  • src / test / java: means for storing test code
  • src / test / resources: resource files used to store test code

3, use our project in the Eclipse IDE

In order to make it an Eclipse project, go to the "HelloWorld" project directory, type the following command:

 

 After executing the above command, which automatically downloads updates related resources and configuration information (waiting period), and produces all project files required by Eclipse IDE. To import the project into the Eclipse IDE:

Select "File -> Import ... -> General-> Existing Projects into Workspace", the "HelloWord projects into Ecli

Project into the Eclipse IDE, as shown:

 

 

4, update the POM file

The default pom.xml too simple, very often, you need to add a compiler plugin to tell Maven which JDK version to compile the project, we use the 4.12 version of junit, and specify which JDK version to use with plug-ins.

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

5, run maven project

Now, we will use the Maven project, and output compiled into a "jar" file. pom.xml file package packaging elements in the definition of what should be the output package. Figure:

 

 

Back to our project directory, enter the command: mvn package

 

 It compiles, run the unit test project and packaged into a jar file and put it in the project / target folder.

 

 Finally, let's run this jar file, look at the results:

 

 

Printout: "HelloWorld".

 

Guess you like

Origin www.cnblogs.com/dyh004/p/11579274.html