Maven Building Project Guidebook

Overview

We all know that in the development of javaEE projects, in addition to managing jars, maven's more important function is to be able to manage projects. That is, the project is generated according to the template. This blog post mainly introduces the whole process of making a maven project template, publishing the project template to the remote private server, and then creating a specific project from the remote private server.

Create a project template

1. Create a maven web project through eclipse, myeclipse or IntelliJ IDEA, and add the code that needs to be used as a template to the project, such as system management functions, template styles, and so on. As shown below:

2. Open the command line, enter the template project folder, and execute the command mvn archetype:create-from-project. The execution result is shown in the following figure:

At this time, maven has generated the template project to the target\generated-sources\archetype directory of the project, as shown in the following figure:

You can enter the target folder and open maven-web-archetype-1.0.jar to see the template project.

Publish template project to remote maven repository

There should be many methods here, only one that has been practiced so far is provided here, which will be added later:

Open the pom file in the maven-web\target\generated-sources\archetype directory, the code 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.maven</groupId>
  <artifactId>maven-web-archetype</artifactId>
  <version>1.0</version>
  <packaging>maven-archetype</packaging>

  <name>maven-web-archetype</name>

  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.0.1</version>
      </extension>
    </extensions>

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.0.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Add the configuration information of the maven remote warehouse and the specific target warehouse to the pom file. The modified pom file 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.maven</groupId>
  <artifactId>maven-web-archetype</artifactId>
  <version>1.0</version>
  <packaging>maven-archetype</packaging>

  <name>maven-web-archetype</name>
<distributionManagement>
		 <repository>
			<id>nexus-3rd-party</id>
			 <name>nexus-3rd-party</name>
			  <url>http://10.18.100.80:8081/repository/nexus-3rd-party/</url>   
		 </repository>
   </distributionManagement>
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.0.1</version>
      </extension>
    </extensions>

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.0.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Enter the maven-web\target\generated-sources\archetype directory from the command line and execute the mvn deploy command directly. The template is installed to the remote private server. The command line execution process is as follows:

E:\01.WorkDocument\workspace\pmp-demo\maven-web\target\generated-sources\archetype>mvn deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-web-archetype 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ maven-web-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 21 resources
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ maven-web-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:jar (default-jar) @ maven-web-archetype ---
[INFO] Building archetype jar: E:\01.WorkDocument\workspace\pmp-demo\maven-web\target\generated-sources\archetype\target\maven-web-archetype-1.0
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:integration-test (default-integration-test) @ maven-web-archetype ---
[INFO] Processing Archetype IT project: basic
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-web-archetype:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: packageInPathFormat, Value: it/pkg
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[INFO] Project created from Archetype in dir: E:\01.WorkDocument\workspace\pmp-demo\maven-web\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ maven-web-archetype ---
[INFO] Installing E:\01.WorkDocument\workspace\pmp-demo\maven-web\target\generated-sources\archetype\target\maven-web-archetype-1.0.jar to D:\.m2\repository\com\maven\maven-web-archetype\1.0\maven-web-archetype-1.0.jar
[INFO] Installing E:\01.WorkDocument\workspace\pmp-demo\maven-web\target\generated-sources\archetype\pom.xml to D:\.m2\repository\com\maven\maven-web-archetype\1.0\maven-web-archetype-1.0.pom
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:update-local-catalog (default-update-local-catalog) @ maven-web-archetype ---
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ maven-web-archetype ---
Uploading: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/1.0/maven-web-archetype-1.0.jar
Uploaded: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/1.0/maven-web-archetype-1.0.jar (9 KB at 43.9 KB/sec)
Uploading: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/1.0/maven-web-archetype-1.0.pom
Uploaded: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/1.0/maven-web-archetype-1.0.pom (2 KB at 7.8 KB/sec)
Downloading: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/maven-metadata.xml
Uploading: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/maven-metadata.xml
Uploaded: http://10.18.100.80:8081/repository/nexus-3rd-party/com/maven/maven-web-archetype/maven-metadata.xml (304 B at 2.2 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.828 s
[INFO] Finished at: 2017-08-15T18:00:18+08:00
[INFO] Final Memory: 16M/194M
[INFO] ------------------------------------------------------------------------

At this time, open the remote private server, we can see the project template:

Create a project from a template

1. Command line method

Just find a file, this folder is used to save the generated project, and the command line enters the folder and executes the command:

mvn archetype:generate -DgroupId=com.maven  -DartifactId=maven-web-demo  -Dpackage=com.maven.demo -Dversion=1.0  -DarchetypeGroupId=com.maven  -DarchetypeArtifactId=maven-web-archetype -DarchetypeVersion=1.0 -DarchetypeRepository=http://10.18.100.80:8081/repository/nexus-3rd-party/  -DinteractiveMode=false -X

The execution process is omitted.

After the execution is completed, the project maven-web-demo has been generated in this folder

As shown below:

Import the project into the IDE tool as shown below;

2. Create a project through the maven plugin

In the idemaven project creation interface, do the following configuration:

After clicking OK, the template project has been integrated into eclipse:

You can create it directly according to the project creation process.

Guess you like

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