maven 使用实例

使用mvn archetype:create -DgroupId=com.jiepu -DartifactId=app -DpackageName=com.jiepu -Dversion=2.0创建maven工程
<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.jiepu</groupId>
  <artifactId>test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test</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>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

使用mvn assembly:assembly 生成有依赖的jar

然后修改jar包的MANIFEST.MF文件、添加

Main-Class: com.jiepu.App

使用java -jar test-1.0-SNAPSHOT-jar-with-dependencies.jar就可以运行

或者使用下面的方法:

<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.jiepu</groupId>
    <artifactId>app</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>app</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>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>   
                    <archive>
                        <manifest>
                            <mainClass> com.jiepu.app.App</mainClass>
                            <packageName>com.jiepu.app</packageName>
                        </manifest>
                    </archive>
                </configuration>

            </plugin>
        </plugins>

    </build>
</project>
<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.jiepu</groupId>
    <artifactId>mvn_echo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>mvn_echo</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>
        <plugins>
            <!--echo内置属性-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Displaying value of pom.xml element 显示内置属性:</echo>
                                <echo>${basedir}</echo>
                                <echo>${project.basedir}</echo>
                                <echo>${version}</echo>
                                <echo>${project.artifactId}</echo>
                                <echo>${project.build.testSourceDirectory}</echo>
                                <echo>${project.build.sourceDirectory}</echo>
                                <echo>${project.build.directory}</echo>
                                <echo>${project.groupId}</echo>
                                <echo>${project.version}</echo>
                                <echo>${project.build.finalName}</echo>
                                <echo>${settings.localRepository}</echo>
                                <echo>${user.home}</echo>
                                <echo>${env.JAVA_HOME}</echo>
                                <echo>${project.outputDirectory}</echo>
                                <echo>${project.testOutputDirectory}</echo>
                                <echo>${env.PATH}</echo>
                                <echo>${os.name}</echo>
                                <echo>${os.arch}</echo>
                                <echo>${os.version}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--mvn site插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <locales>zh_CN</locales>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
           <!--javadoc插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.7</version>
            </plugin>
             <!--源码交叉 xref插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
            </plugin>
             <!--checkstyle插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.5</version>
            </plugin>
             <!--pmd插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
            </plugin>
             <!--cobertura测试覆盖率报告插件-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.4</version>
            </plugin>

        </plugins>
    </reporting>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>


</project>
跳过测试:

mvn install -Dmaven.test.skip=true

你可能感兴趣的:(maven 使用实例)