我们在写代码的时候,有些项目会有重复代码,或者是重复项目结构,这样我们就可以用maven 生成一个项目的基本骨架,就像我之前介绍的哪个logindemo一样继承了webApp-achetype一样。我们也可以生成我们自己的项目骨架。
准备下我们的项目骨架结构:看图:
看下我这边的已有的代码:
我的这个程序是一个普通http接口。然后往往在公司会有很多这样的接口,然后每个接口都去实现同样的一份代码程序会感觉很费时间,包括拷贝代码建立项目目录等,都会很花费时间的,通用的代码可以做成jar 被依赖继承,但是那些不通用的地方就需要自己实现了,但是自己实现的项目结构往往是一样的,这样就可以用到这个骨架了。
来看下我这pom.xml文件:
<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.0modelVersion>
<groupId>com.nnk.templategroupId>
<artifactId>templateartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>XLTDRecharge Maven Webappname>
<url>http://maven.apache.orgurl>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.1.2.RELEASEversion>
<relativePath />
parent>
<distributionManagement>
<repository>
<id>nnk-releasesid>
<name>NNK Releases Repositoryname>
<url>${repositories.releases.url}url>
repository>
<snapshotRepository>
<id>nnk-snapshotsid>
<name>NNK Snapshots Repositoryname>
<url>${repositories.snapshots.url}url>
snapshotRepository>
distributionManagement>
<properties>
<java.version>1.6java.version>
<start-class>com.nnk.template.recharge.Mainstart-class>
<main.basedir>${basedir}/../..main.basedir>
<m2eclipse.wtp.contextRoot>/m2eclipse.wtp.contextRoot>
<httpclient.version>4.4.1httpclient.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>log4j-over-slf4jartifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-log4jartifactId>
<exclusions>
<exclusion>
<artifactId>log4jartifactId>
<groupId>log4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.6version>
dependency>
<dependency>
<groupId>org.jvnet.hudson.dom4jgroupId>
<artifactId>dom4jartifactId>
<version>1.6.1-hudson-3version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.16version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.3version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>fluent-hcartifactId>
<version>${httpclient.version}version>
<exclusions>
<exclusion>
<artifactId>commons-loggingartifactId>
<groupId>commons-logginggroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.2.4version>
dependency>
<dependency>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
<version>15.0version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>${httpclient.version}version>
<exclusions>
<exclusion>
<artifactId>commons-codecartifactId>
<groupId>commons-codecgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpcoreartifactId>
<version>${httpclient.version}version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpmimeartifactId>
<version>${httpclient.version}version>
dependency>
<dependency>
<groupId>com.nnk.basegroupId>
<artifactId>msgsrv-toolsartifactId>
<version>2.4version>
dependency>
<dependency>
<groupId>com.nnk.transportgroupId>
<artifactId>interface-extern-springartifactId>
<version>1.3version>
dependency>
<dependency>
<groupId>com.nnk.basegroupId>
<artifactId>msgsrv-clientartifactId>
<version>1.3version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
<build>
<finalName>LMRechargefinalName>
<sourceDirectory>src/main/javasourceDirectory>
<testSourceDirectory>src/test/javatestSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.6source>
<target>1.6target>
<encoding>GBKencoding>
configuration>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<classifier>bootclassifier>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>2.4version>
<executions>
<execution>
<id>interface-extern-springid>
<goals><goal>jargoal>goals>
<phase>packagephase>
<configuration>
<excludes>
<exclude>**/src/test/resource/**exclude>
<exclude>**/*Test.javaexclude>
excludes>
configuration>
execution>
executions>
<configuration>
<excludes>
<exclude>src/main/resourcesexclude>
<exclude>src/test/resourceexclude>
<exclude>**/*Test.javaexclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
这里面别的我就不介绍了,主要讲下这段配置
<distributionManagement>
<repository>
<id>nnk-releasesid>
<name>NNK Releases Repositoryname>
<url>${repositories.releases.url}url>
repository>
<snapshotRepository>
<id>nnk-snapshotsid>
<name>NNK Snapshots Repositoryname>
<url>${repositories.snapshots.url}url>
snapshotRepository>
distributionManagement>
这里主要是deploy和install用到个人私服仓库,这里会指定你上传到哪里的url 。我的url在maven 配置settings文件里面配置了,所以这里只要用${引用就可以了};
如果没有settings.xml中没有配置就需要这样配置了:
<repositories>
<repository>
<id>local-nexusid>
<name>private repository nexusname>
<url>http://xxx:8081/nexus/content/groups/public/url>
<releases>
releases>
<snapshots>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>local-nexusid>
<name>private plugin nexusname>
<url>http://xxx:8081/nexus/content/groups/public/url>
<releases>
releases>
<snapshots>
snapshots>
pluginRepository>
pluginRepositories>
<distributionManagement>
<repository>
<id>releasesid>
<name>Nexus Release Repositoryname>
<url>http://xxx:8081/nexus/content/repositories/releases/url>
repository>
<snapshotRepository>
<id>snapshotsid>
<name>Nexus Snapshot Repositoryname>
<url>http://xxx:8081/nexus/content/repositories/snapshots/url>
snapshotRepository>
distributionManagement>
<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.0modelVersion>
<groupId>com.nnk.templategroupId>
<artifactId>template-archetypeartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>maven-archetypepackaging>
<name>template-archetypename>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetypegroupId>
<artifactId>archetype-packagingartifactId>
<version>2.4version>
extension>
extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-pluginartifactId>
<version>2.4version>
plugin>
plugins>
pluginManagement>
build>
<description>Spring Boot Starter Parentdescription>
<url>http://maven.apache.orgurl>
<developers>
<developer>
<id>pwebbid>
<name>Phillip Webbname>
<email>pwebb at gopivotal.comemail>
<organization>Pivotal Software, Inc.organization>
<organizationUrl>http://www.spring.ioorganizationUrl>
<roles>
<role>Project leadrole>
roles>
developer>
<developer>
<id>dsyerid>
<name>Dave Syername>
<email>dsyer at gopivotal.comemail>
<organization>Pivotal Software, Inc.organization>
<organizationUrl>http://www.spring.ioorganizationUrl>
<roles>
<role>Project leadrole>
roles>
developer>
developers>
<licenses>
<license>
<name>Apache License, Version 2.0name>
<url>http://www.apache.org/licenses/LICENSE-2.0url>
license>
licenses>
<scm>
<url>https://github.com/spring-projects/spring-boot/spring-boot-starter-parent/templateurl>
scm>
project>
如果此时mvn deploy 的化会出现Error 如下图
并没有下面这个:
<distributionManagement>
<repository>
<id>nnk-releasesid>
<name>NNK Releases Repositoryname>
<url>${repositories.releases.url}url>
repository>
<snapshotRepository>
<id>nnk-snapshotsid>
<name>NNK Snapshots Repositoryname>
<url>${repositories.snapshots.url}url>
snapshotRepository>
distributionManagement>
这时就需要在pom.xml里面加上这段:
完整的应该是:
<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.0modelVersion>
<groupId>com.nnk.templategroupId>
<artifactId>template-archetypeartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>maven-archetypepackaging>
<name>template-archetypename>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetypegroupId>
<artifactId>archetype-packagingartifactId>
<version>2.4version>
extension>
extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-pluginartifactId>
<version>2.4version>
plugin>
plugins>
pluginManagement>
build>
<description>Spring Boot Starter Parentdescription>
<url>http://maven.apache.orgurl>
<developers>
<developer>
<id>pwebbid>
<name>Phillip Webbname>
<email>pwebb at gopivotal.comemail>
<organization>Pivotal Software, Inc.organization>
<organizationUrl>http://www.spring.ioorganizationUrl>
<roles>
<role>Project leadrole>
roles>
developer>
<developer>
<id>dsyerid>
<name>Dave Syername>
<email>dsyer at gopivotal.comemail>
<organization>Pivotal Software, Inc.organization>
<organizationUrl>http://www.spring.ioorganizationUrl>
<roles>
<role>Project leadrole>
roles>
developer>
developers>
<licenses>
<license>
<name>Apache License, Version 2.0name>
<url>http://www.apache.org/licenses/LICENSE-2.0url>
license>
licenses>
<distributionManagement>
<repository>
<id>nnk-releasesid>
<name>NNK Releases Repositoryname>
<url>${repositories.releases.url}url>
repository>
<snapshotRepository>
<id>nnk-snapshotsid>
<name>NNK Snapshots Repositoryname>
<url>${repositories.snapshots.url}url>
snapshotRepository>
distributionManagement>
<scm>
<url>https://github.com/spring-projects/spring-boot/spring-boot-starter-parent/templateurl>
scm>
project>
然后再 mvn deploy 之后就把这个骨架上传到我们的私服仓库了。
我新建一个项目:
点击ok 然后:
之后的步骤就不多介绍了和前面logindemo的步骤是一样的。
最终生成的项目结构就是这样子的:
这样我们的项目结构骨架就是这样生成了,这样只需要在项目骨架上面做开发了,是不是会爽很多,
有什么不懂得地方欢迎和我交流,