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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值