我们在写代码的时候,有些项目会有重复代码,或者是重复项目结构,这样我们就可以用maven 生成一个项目的基本骨架,就像我之前介绍的哪个logindemo一样继承了webApp-achetype一样。我们也可以生成我们自己的项目骨架。
准备下我们的项目骨架结构:看图:
看下我这边的已有的代码:
我的这个程序是一个普通http接口。然后往往在公司会有很多这样的接口,然后每个接口都去实现同样的一份代码程序会感觉很费时间,包括拷贝代码建立项目目录等,都会很花费时间的,通用的代码可以做成jar 被依赖继承,但是那些不通用的地方就需要自己实现了,但是自己实现的项目结构往往是一样的,这样就可以用到这个骨架了。
来看下我这pom.xml文件:
<?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.nnk.template</groupId>
<artifactId>template</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>XLTDRecharge Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.2.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<distributionManagement>
<repository>
<id>nnk-releases</id>
<name>NNK Releases Repository</name>
<url>${repositories.releases.url}</url>
</repository>
<snapshotRepository>
<id>nnk-snapshots</id>
<name>NNK Snapshots Repository</name>
<url>${repositories.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
<properties>
<java.version>1.6</java.version>
<start-class>com.nnk.template.recharge.Main</start-class>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
<httpclient.version>4.4.1</httpclient.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-jasper</artifactId>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>javax.servlet</groupId>-->
<!--<artifactId>jstl</artifactId>-->
<!--</dependency>-->
<!--httpClient dependency start-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1-hudson-3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>commons-codec</groupId>-->
<!--<artifactId>commons-codec</artifactId>-->
<!--<version>1.6</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.nnk.base</groupId>
<artifactId>msgsrv-tools</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.nnk.transport</groupId>
<artifactId>interface-extern-spring</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.nnk.base</groupId>
<artifactId>msgsrv-client</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<finalName>LMRecharge</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>GBK</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>boot</classifier>
</configuration>
</plugin>
<!--生成可执行JAR包命令 maven-jar-plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>interface-extern-spring</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<excludes>
<exclude>**/src/test/resource/**</exclude>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
<configuration>
<excludes>
<exclude>src/main/resources</exclude>
<exclude>src/test/resource</exclude>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
这里面别的我就不介绍了,主要讲下这段配置
<distributionManagement>
<repository>
<id>nnk-releases</id>
<name>NNK Releases Repository</name>
<url>${repositories.releases.url}</url>
</repository>
<snapshotRepository>
<id>nnk-snapshots</id>
<name>NNK Snapshots Repository</name>
<url>${repositories.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
这里主要是deploy和install用到个人私服仓库,这里会指定你上传到哪里的url 。我的url在maven 配置settings文件里面配置了,所以这里只要用${引用就可以了};
如果没有settings.xml中没有配置就需要这样配置了:
<repositories>
<repository>
<id>local-nexus</id>
<name>private repository nexus</name>
<url>http://xxx:8081/nexus/content/groups/public/</url>
<releases>
</releases>
<snapshots>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local-nexus</id>
<name>private plugin nexus</name>
<url>http://xxx:8081/nexus/content/groups/public/</url>
<releases>
</releases>
<snapshots>
</snapshots>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://xxx:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://xxx:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
step 2 :接下来就是生成这个项目骨架了
- cmd到项目目录,mvn clean,然后 mvn archetype:create-from-project
- 成功之后cd到target/generated-sources/archetype, mvn clean compile
然后查看下这个目录下有什么东西
3.然后检查下target/generated-sources/archetype这个目录下这pom.xml文件
<?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.nnk.template</groupId>
<artifactId>template-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<name>template-archetype</name>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>2.4</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<description>Spring Boot Starter Parent</description>
<url>http://maven.apache.org</url>
<developers>
<developer>
<id>pwebb</id>
<name>Phillip Webb</name>
<email>pwebb at gopivotal.com</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>http://www.spring.io</organizationUrl>
<roles>
<role>Project lead</role>
</roles>
</developer>
<developer>
<id>dsyer</id>
<name>Dave Syer</name>
<email>dsyer at gopivotal.com</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>http://www.spring.io</organizationUrl>
<roles>
<role>Project lead</role>
</roles>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<scm>
<url>https://github.com/spring-projects/spring-boot/spring-boot-starter-parent/template</url>
</scm>
</project>
如果此时mvn deploy 的化会出现Error 如下图
并没有下面这个:
<distributionManagement>
<repository>
<id>nnk-releases</id>
<name>NNK Releases Repository</name>
<url>${repositories.releases.url}</url>
</repository>
<snapshotRepository>
<id>nnk-snapshots</id>
<name>NNK Snapshots Repository</name>
<url>${repositories.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
这时就需要在pom.xml里面加上这段:
完整的应该是:
<?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.nnk.template</groupId>
<artifactId>template-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>
<name>template-archetype</name>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>2.4</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<description>Spring Boot Starter Parent</description>
<url>http://maven.apache.org</url>
<developers>
<developer>
<id>pwebb</id>
<name>Phillip Webb</name>
<email>pwebb at gopivotal.com</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>http://www.spring.io</organizationUrl>
<roles>
<role>Project lead</role>
</roles>
</developer>
<developer>
<id>dsyer</id>
<name>Dave Syer</name>
<email>dsyer at gopivotal.com</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>http://www.spring.io</organizationUrl>
<roles>
<role>Project lead</role>
</roles>
</developer>
</developers>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<distributionManagement>
<repository>
<id>nnk-releases</id>
<name>NNK Releases Repository</name>
<url>${repositories.releases.url}</url>
</repository>
<snapshotRepository>
<id>nnk-snapshots</id>
<name>NNK Snapshots Repository</name>
<url>${repositories.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
<scm>
<url>https://github.com/spring-projects/spring-boot/spring-boot-starter-parent/template</url>
</scm>
</project>
然后再 mvn deploy 之后就把这个骨架上传到我们的私服仓库了。
step 3:怎么使用这个骨架
我新建一个项目:
点击ok 然后:
之后的步骤就不多介绍了和前面logindemo的步骤是一样的。
最终生成的项目结构就是这样子的:
这样我们的项目结构骨架就是这样生成了,这样只需要在项目骨架上面做开发了,是不是会爽很多,
有什么不懂得地方欢迎和我交流,