Maven搭建项目指导手册

概述

大家都知道,在javaEE项目的开发中,maven除了管理jar之外,更重好的功能就是能够进行项目管理。即依据模板生成项目。本博文就主要介绍从制作maven项目模板,将项目模板发布到远程私服,再从远程私服创建具体项目的整个过程。

创建项目模板

1、通过eclipse、myeclipse或IntelliJ IDEA创建maven的web项目,将需要作为模板的代码添加到项目中,如系统管理功能,模板样式等等。如下图所示:

2、打开命令行,进入该模板项目文件夹,执行命令mvn archetype:create-from-project,执行结果如下图所示:

这时,maven已经将模板项目生成到该项目的target\generated-sources\archetype目录下,如下图所示:

可以进入target文件夹,打开maven-web-archetype-1.0.jar看看模板项目。

将模板项目发布至远程maven仓库

这里应该有很多种方法,这里只提供目前实践过的一种,后续会补充:

打开maven-web\target\generated-sources\archetype目录下的pom文件,代码如下:

<?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>

在pom文件中增加maven远程仓库的配置信息,及具体目标仓库,修改后的pom文件如下:

<?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>

命令行进入maven-web\target\generated-sources\archetype目录,直接执行mvn deploy命令,模板即安装至远程私服,命令行执行过程如下:

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] ------------------------------------------------------------------------

这时,打开远程私服,我们可以看到该项目模板:

依据模板创建项目

1、命令行方法

随便找个文件件,这个文件夹用来保存生成的项目,命令行进入该文件夹下执行命令:

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

执行过程省略。

执行完成后,已经在该文件夹下生成了项目maven-web-demo

如下图所示:

将该项目导入到ide工具,如下图所示;

2、通过maven插件创建工程

在idemaven项目创建界面,做如下配置:

点击OK后模板项目已经集成到eclipse中:

直接按照项目创建流程创建即可。

猜你喜欢

转载自my.oschina.net/u/195935/blog/1510656