小言_互联网的博客

Maven archetype构建java项目模板

473人阅读  评论(0)

1 原型命令及参数

  • 命令
mvn archetype:generate
  • 参数
序号 参数 描述
1 DgroupId 组织名,公司名逆序.项目名
2 DartifactId 项目名
3 DarchetypeArtifactId 指定ArchetypeId创建不同项目,简单java应用:maven-archetype-quickstart,webapp:maven-archetype-webapp
4 DinteractiveMode 是否使用交互模式,是否开启命令行填写配置

2 新建项目

2.1 java应用项目

  • 新建命令
mvn archetype:generate \
-DgroupId=com.company.dataTest \
-DartifactId=dataStructure \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
  • 提示信息
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/xdq/xinPrj/java/javaAppTest
[INFO] Parameter: package, Value: com.company.dataTest
[INFO] Parameter: groupId, Value: com.company.dataTest
[INFO] Parameter: artifactId, Value: dataStructure
[INFO] Parameter: packageName, Value: com.company.dataTest
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/xdq/xinPrj/java/javaAppTest/dataStructure
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.714 s
[INFO] Finished at: 2019-09-23T14:33:09+08:00
[INFO] ------------------------------------------------------------------------

2.1.0 文件结构

└── dataStructure
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   └── java
    │   │       └── com
    │   │           └── company
    │   │               └── dataTest
    │   │                   └── App.java
    │   └── test
    │       └── java
    │           └── com
    │               └── company
    │                   └── dataTest
    │                       └── AppTest.java
    └── target
        ├── classes
        │   └── com
        │       └── company
        │           └── dataTest
        │               └── App.class
        └── test-classes
            └── com
                └── company
                    └── dataTest
                        └── AppTest.class

2.1.2 文件内容

  • App.java
package com.company.dataTest;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}
  • AppTest.java
package com.company.dataTest;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

2.1.3 运行

  • 运行编译的类文件
    执行target中编译好的*.class
cd /home/xdq/xinPrj/java/javaAppTest/dataStructure/target/classes
java com.company.dataTest.App
  • 结果
Hello World!
  • 配置
    在pom.xml文件中添加插件:maven-shade-plugin,添加主类入口.
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>2.1</version>
	<executions>
		<execution>
			<phase>package</phase>
				<goals>
					<goal>shade</goal>
				</goals>
				<configuration>
					<transformers>
						<transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.company.dataTest.App</mainClass>
						</transformer>
					</transformers>
				</configuration>
		</execution>
	</executions>
</plugin>
  • 打包
mvn package
  • 运行
cd /home/xdq/xinPrj/java/javaAppTest/dataStructure/target
java -jar dataStructure-1.0-SNAPSHOT.jar
  • 结果
Hello World!

2.2 java web项目

  • 新建
mvn archetype:generate \
-DgroupId=com.company.spring \
-DartifactId=springFramework \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false
  • 提示信息
    其中basedir为/home/xdq/xinPrj/java/javaWebTest,项目的路径.
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 898 B/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (3.9 kB at 11 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/xdq/xinPrj/java/javaWebTest
[INFO] Parameter: package, Value: com.company.spring
[INFO] Parameter: groupId, Value: com.company.spring
[INFO] Parameter: artifactId, Value: springFramework
[INFO] Parameter: packageName, Value: com.company.spring
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/xdq/xinPrj/java/javaWebTest/springFramework
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.307 s
[INFO] Finished at: 2019-09-23T14:42:25+08:00
[INFO] ------------------------------------------------------------------------

2.2.1 文件结构

├── springFramework
│   ├── pom.xml
│   └── src
│       └── main
│           ├── resources
│           └── webapp
│               ├── index.jsp
│               └── WEB-INF
│                   └── web.xml

2.2.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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company.spring</groupId>
  <artifactId>springFramework</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springFramework Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>springFramework</finalName>
  </build>
</project>
  • index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

2.2.3 运行

  • 打包
mvn clean package
  • 生成target文件
target
│       ├── classes
│       ├── maven-archiver
│       │   └── pom.properties
│       ├── springFramework
│       │   ├── index.jsp
│       │   ├── META-INF
│       │   └── WEB-INF
│       │       ├── classes
│       │       └── web.xml
│       └── springFramework.war
  • 直接tomcat运行
    将生成的*.war放入tomcat安装文件的webapps中,启动tomcat.
http://localhost:8080/springFramework/index.jsp
  • visualcode调试运行
    添加tomcat和war.
图2.1 VisualCode配置tomcat

图.2 VisualCode调试Tomcat

在visualcode中运行tomcat,访问localhost:8080,出现上图2.2的显示结果,点击相应的服务即可跳转到相应首页.


【参考文献】
[1]https://www.runoob.com/maven/maven-creating-project.html
[2]https://www.runoob.com/maven/maven-web-application.html
[3]https://spring.io/guides/gs/spring-boot/


转载:https://blog.csdn.net/Xin_101/article/details/101202541
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场