您的位置:首页 > 其它

Maven-Archetype自定义项目脚手架(一)

2016-09-25 22:22 281 查看
开始弄...

一、创建项目

新建一个普通的maven项目工程

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.0</modelVersion>

<groupId>com.zhouxq</groupId>
<artifactId>maven-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>maven-archetype</name>
<url>http://maven.apache.org</url>
</project>
二、定义所需项目的结构

在src/main/resouces/archetype-resouces下创建自己的项目结构,src/main/resouces/META-INF/maven下配置好自己项目的目录结构,如下参考:



三、定义项目所需配置

1.App.java

package ${package}.demo;

public class App {

public static void main( String[] args )
{
System.out.println( "Hello World!" );
}

}

2.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.0</modelVersion>

<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>

<name>${artifactId}</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.archetype-metadata.xml

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="app-server">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="false" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>

<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
</fileSets>
<requiredProperties>
<!--由于是由Velocity引擎生成的,所以key不能带有"."-->
<requiredProperty key="groupId">
<defaultValue>com.zhouxq</defaultValue>
</requiredProperty>
</requiredProperties>
</archetype-descriptor>
四、安装

mvn install

五、使用

mvn archetype:generate \

-DarchetypeGroupId=com.zhouxq \

-DarchetypeArtifactId=maven-archetype \

-DarchetypeVersion=.1.0-SNAPSHOT \

-DgroupId=<your.groupId>

-DartifactId=<your.artifactId>

-Dversion=<your.version>

参考的一个指导:http://marosmars.weebly.com/blog/maven-archetype-tutorial
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven