Maven教程

Maven示例

我们可以通过执行 mvn工具 archetype: generate 命令来创建一个简单的maven示例。
要使用maven创建一个简单的Java项目,您需要打开命令提示符并运行mvn工具的 archetype: generate 命令。
语法
用于生成项目体系结构的 语法如下所示:
mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid 
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=booleanValue
示例
用于生成项目体系结构的 示例如下所示:
mvn archetype:generate -DgroupId=com.lidihuo -DartifactId=CubeGenerator 
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
注意: 在这里,我们使用maven-archetype-quickstart创建简单的maven核心项目。如果使用maven-archetype-webapp,它将生成一个简单的maven Web应用程序。
输出
现在它将 在命令提示符下生成以下代码:
mvn archetype:generate -DgroupId=com.lidihuo -DartifactId=Cub
eGenerator -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=fa
lse
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>
>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave
n-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 3.5 KB/se
c)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave
n-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 0.9 KB/s
ec)
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.lidihuo
[INFO] Parameter: packageName, Value: com.lidihuo
[INFO] Parameter: package, Value: com.lidihuo
[INFO] Parameter: artifactId, Value: CubeGenerator
[INFO] Parameter: basedir, Value: C:\Users\SSS IT
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\SSS IT\CubeGene
rator
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.913s
[INFO] Finished at: Thu Dec 26 16:45:18 IST 2013
[INFO] final Memory: 9M/25M
[INFO] ------------------------------------------------------------------------
'cmd' is not recognized as an internal or external command,
operable program or batch file.
生成的目录结构
现在转到执行mvn命令的当前目录。例如: C:\Users\SSS IT\CubeGenerator 。您将看到创建了一个具有以下目录的简单Java项目:
CubeGenerator
-src
--main
---java
----com
-----lidihuo
------App.java
--test
---java
----com
-----lidihuo
------AppTest.java
-pom.xml
如您所见,创建了3个文件pom.xml,App.java和AppTest.java。让我们快速看一下这些文件:
1、自动生成的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.lidihuo</groupId>
  <artifactId>CubeGenerator</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CubeGenerator</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>
</project>
2、自动生成的App.java文件
package com.lidihuo;
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}
3、自动生成的AppTest.java文件
package com.lidihuo;
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 );
    }
}

编译Maven Java项目

要编译项目,请转到项目目录,
例如: C:\Users\SSS IT\CubeGenerator ,然后在命令提示符下输入以下命令:
mvn clean compile
现在,您将在命令提示符下看到很多执行。如果检查项目目录,则会创建包含类文件的 目标目录

运行Maven Java项目

要运行项目,请转到项目目录\target\classes,
例如: C:\Users\SSS IT\CubeGenerator\target\classes ,然后在命令提示符下输入以下命令:
java com.lidihuo.App
现在,您将在命令提示符下看到输出:

maven示例的输出

Hello World!

如何构建Maven项目或如何打包Maven项目?

mvn软件包命令完成了Maven的构建生命周期。项目,例如:
验证 编译 测试 打包 集成测试 验证 安装 部署
访问此链接以了解有关构建生命周期的更多信息 http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
您需要在命令提示符下执行以下命令来打包Maven项目:
 mvn package
现在您将看到在项目/目标目录中创建了 一个jar文件
您还可以通过jar文件运行maven项目。为此,请转到Maven项目目录,例如: C:\Users\SSS IT\CubeGenerator 并在cmd上执行以下命令:
java -classpath target\CubeGenerator-1.0-SNAPSHOT.jar;.; com.lidihuo.App
现在您将看到以下输出:
Hello World!
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4