Maven鸡毛蒜皮

maven坐标规划原则(项目命名规范)

使用groupId定义项目;使用artifactId定义模块

e.g
组织域名: zoo.com
项目名: cat 模块名: dao
项目坐标:

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

使用此种规划可有效防止混淆,使项目结构清晰。

maven项目版本样式

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

主版本 -- 大型架构变更
次版本 -- 特性增加
增量版本 -- bug修复
限定符 -- alpha,beta....
打包时还会用到classifier, 如: dog-cli-1.0-sources.jar,dog-cli-1.0-javadoc.jar
其中的sources和javadoc 就是classifier。可以使用Maven Assembly Plugin 来生成。

文件编码警告

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

pom.xml中加入:

<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" 警告

<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 Profiles

#打war包

<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>

转载于:https://my.oschina.net/CasparLi/blog/739416

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值