首页 > 代码库 > 回顾:maven配置和常用命令整理

回顾:maven配置和常用命令整理

  1. 推荐两个库地址,开源中国的好像不好使了

    阿里的仓库:http://maven.aliyun.com/nexus/content/groups/public/

    另一个:http://repo2.maven.org/maven2/

  2.打包

  • maven-compile-plugin 
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
           <configuration>
               <source>1.7</source>
               <target>1.7</target>
               <encoding>utf-8</encoding>
            </configuration>
    </plugin>    

    这个插件可以指定jdk版本和字符集,防止编译时乱码

  • maven-surefire-plugin
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <skip>true</skip>
       <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin>

    可以跳过测试,事实上还可以用include和exclude指定包括哪些测试以及不运行哪些测试,如果测试中有乱码可以指定字符集

  • maven-assembly-plugin
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
            <finalName>tcp</finalName>
            <appendAssemblyId>false</appendAssemblyId>
            <encoding>utf-8</encoding>
            <descriptors>
                <descriptor>src/main/assembly/assembly.xml</descriptor>
            </descriptors>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    就是能将各个包单独打到指定目录的工具,详见:

  • maven-shade-plugin
  • <plugin>
           <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
    </plugin>

    将各个依赖包都打入工程包,生成单独可执行jar

  • build resources
             <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                    <targetPath>${project.build.directory}/classes</targetPath>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**</include>
                    </includes>
                    <targetPath>${project.build.directory}/classes</targetPath>
                </resource>
            </resources>

    这个不是插件,构建时指定一些文件打入jar包

   3.命令

    mvn test 运行测试

    mvn compiler 运行编译

    mvn package 打包

    mvn install 打包并发布到本地仓库

    mvn deploy 打包并发布到远程仓库,仓库配置:

<repository>
            <id>public</id>
            <name>public</name>
            <url>http://10.10.10.10:8081/nexus/content/groups/public</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>releases</id>
            <name>releases</name>
            <url>http://10.10.10.10:8081/nexus/content/repositories/releases</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

    mvn clean 清除运行文件

    mvn eclipse:eclipse 转化为eclipse项目

    mvn idea:idea 转化为idea项目

    mvn test -compile 编译测试代码

    mvn jetty:run jetty运行

    maven可以进行很多拼接命令,比如

    mvn clean compile install deploy -Dmaven.test.skip=true 这个命令就是先清除,再编译打包,发布到本地,发布到远程,并且跳过测试.

    待续...

    

 

回顾:maven配置和常用命令整理