Maven雞毛蒜皮

maven座標規劃原則(項目命名規範)

使用groupId定義項目;使用artifactId定義模塊html

e.g
組織域名: zoo.com
項目名: cat 模塊名: dao
項目座標:java

<groupId>com.zoo.cat</groupId>
<artifactId>cat-dao</artifactId>
<version>1.1.1-alpha</version>

使用此種規劃可有效防止混淆,使項目結構清晰。web

maven項目版本樣式

<主版本>.<次版本>.<增量版本>-<限定符>

主版本 -- 大型架構變動
次版本 -- 特性增長
增量版本 -- bug修復
限定符 -- alpha,beta....
打包時還會用到classifier, 如: dog-cli-1.0-sources.jar,dog-cli-1.0-javadoc.jar
其中的sources和javadoc 就是classifier。可使用Maven Assembly Plugin 來生成。shell

文件編碼警告

[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!‍‍

pom.xml中加入:apache

<properties>
    <project.build.sourceEncoding>GBK</project.build.sourceEncoding>
</properties>

打war包錯誤(找不到web.xml)

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.5:war (default-war) on project WlanTroubleshootingQuery: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

配置插件屬性,加入failOnMissingWebXml字段:架構

<plugin>
<!--http://maven.apache.org/plugins/maven-war-plugin/-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
      <warName>troubleshooting</warName>
      <failOnMissingWebXml>false</failOnMissingWebXml>
      <webResources>
            <resource>
                  <directory>${project.basedir}/src/main/resources</directory>
                  <targetPath>WEB-INF/classes</targetPath>
            </resource>
      </webResources>
      <executions>
            <execution>
                  <id>create-war-file</id>
                  <phase>package</phase><!-- 要綁定到的生命週期的階段 -->
                  <goals>
                        <goal>war</goal><!-- 要綁定的插件的目標 -->
                  </goals>
            </execution>
       </executions>
</configuration>
</plugin>

可執行jar包

<!--pom.xml-->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.ghca.Helloworld</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打印依賴樹

mvn dependency:tree

修改默認的編譯版本

maven默認編譯版本時jdk1.5. 有時候忘記修改就會編譯失敗. 修改setting.xml文件 配置默認profile可解決. 這裏同時配置了文件編碼. 解決 "[WARNING] Using platform encoding" 警告app

<profiles>
    <profile>
        <id>UTF8-JDK1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk> <!-- 當jdk爲1.8時本profile生效 -->
        </activation>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
</profiles>

參考: Introduction to Build Profileswebapp

#打war包maven

<dependencies>
    <!-- 本地包 -->
    <dependency>
        <groupId>com.qq</groupId>
        <artifactId>qq_connect</artifactId>
        <version>2.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/QQSdk4J.jar</systemPath>
    </dependency>
</dependencies>
<build>
    <finalName>AuthServer</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <!--JDK版本-->
                <source>1.7</source>
                <target>1.7</target>
                <!--編譯使用的字符集-->
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <!--http://maven.apache.org/plugins/maven-war-plugin/-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <!--war包文件名-->
                <warName>authServer</warName>
                <!--指定web.xml文件位置-->
                <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                <!--指定各類資源文件-->
                <webResources>
                    <resource>
                        <directory>${project.basedir}/src/main/resources</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>
相關文章
相關標籤/搜索