1. 程式人生 > >(二)Java工程化--Maven實踐

(二)Java工程化--Maven實踐

創建項目 artifact local res path 默認 mojo exception 2-0

Maven項目版本號

  • 默認版本號: 1.0-SNAPSHOT 最佳實踐是約定該版本為不穩定版本,如果發布一定要刪除;
  • 建議的版本規則: 主版本號.次版本號.增量版本號-<裏程碑版本> 如:1.0.0-RELEASE 10.2.5-FINAL 等. 最佳實踐是結合自身情況制定大家都認可的版本號規則.

常見命令

內置的maven插件提供了常見的命令, 可以在以下位置找到對應的包: .m2\repository\org\apache\maven\plugins

  • compile
  • clean 刪除/target,將已編譯的二進制文件等刪除
  • test test case junit/testNG
  • package 打包
  • install 把項目install到本地倉庫
  • deploy 發布jar到remote服務器
  • mvn help:system 查看環境變量

插件

插件倉庫

  • https://maven.apache.org/plugins/
  • http://www.mojohaus.org/plugins.html
常見插件
  • findbugs 靜態代碼檢查
  • versions 統一升級版本號 統一升級版本 http://www.mojohaus.org/versions-maven-plugin/ 可以查看使用示例, 常用的設置版本的命令為mvn versions: set –DnewVersion=1.1.0-final
  • mvn versions:set -DnewVersion=1.1
  • source 打包源代碼,當jar提供給外部的時候斟酌使用
  • assembly 打包zip、war
  • tomcat7
<plugins>
    <!--靜態代碼bug掃描-->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
            <threshold>High</threshold>
            <effort>Default</effort>
            <findbugsXmlOutput>true</findbugsXmlOutput>
            <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
        </configuration>
    </plugin>
    <!--版本號管理-->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <version>2.3</version>
    </plugin>
    <!--打包源代碼-->
    <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <id>attach-sources</id>
                <phase>install</phase>
                <goals>
                    <goal>jar-no-fork</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <!--這個有點暈,生成可執行jar包什麽的-->
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
            <archieve>
                <manifest>
                    <mainClass>com.xlx.Test</mainClass>
                </manifest>
            </archieve>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
    <!--tomcat插件-->
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <port>8080</port>
            <path>/</path>
        </configuration>
    </plugin>
</plugins>

自定義插件

學習地址https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

  1. 創建項目
  2. 修改pom的<packaging>maven-plugin</packaging>
  3. 添加依賴
<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>LATEST</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
  1. 寫代碼實現AbstractMojo
@Mojo(name="xlxTest",defaultPhase = LifecyclePhase.PACKAGE)
public class Test extends AbstractMojo {

    /**
     * 接收的參數
     */
    @Parameter
    private String message;

    /**
     * 接收多個值的參數
     */
    @Parameter
    private List<String> options;

    /**
     * 命令行中接收,註意必須有property mvn:package -Dargs=this is from cmd
     */
    @Parameter(property = "args")
    private String args;

    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println("my first maven plugin message is : " + message);
        System.out.println("my first maven plugin options is : " + options);
        System.out.println("my first maven plugin args from evm is : " + args);
    }
}
  1. mvn install
  2. 使用, maven可以接收參數, 也可以使用環境變量取,如${settings.localRepository},${project.baseUri}等
<!--項目pom修改-->
<build>
    <plugins>
        <plugin>
            <groupId>com.xlx</groupId>
            <artifactId>engineering</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>xlxTest</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <message>message</message>
                <options>
                    <option>one</option>
                    <option>two</option>
                </options>
            </configuration>
        </plugin>
    </plugins>
</build>

Profile

  • 不同運行環境 dev/prod/test等
  • mvn clean package –P dev
  • settings.xml 可以指定不同服務器倉儲配置<profile.active>私服或者官方</profile.active>

多環境配置的配置文件路徑
技術分享圖片

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>${baseDir}/src/main/resources</directory>
            <excludes>
                <exclude>conf/**</exclude>
            </excludes>
        </resource>
         <resource>
            <directory>src/main/resources/conf/${profile.active}</directory>
        </resource>
    </resources>
</build>

私服

  • 下載: https://www.sonatype.com/download-oss-sonatype?hsCtaTracking=920dd7b5-7ef3-47fe-9600-10fecad8aa32%7Cf59d5f10-099f-4c66-a622-0254373f4a92
  • 安裝, 解壓即可, 如果需要修改端口號等信息, 可以修改文件\nexus-3.13.0-01\etc\nexus-default.properties
  • 啟動命令 轉到\nexus-3.13.0-01\bin下, nexus /run 可查看啟動日誌
  • 使用: http://books.sonatype.com/nexus-book/reference3/index.html

pom中增加發布節點

<distributionManagement>
    <repository>
        <id>nexus-release</id>
        <name>nexus-release</name>
        <url>http://localhost:8099/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshot</id>
        <name>nexus-snapshot</name>
        <url>http://localhost:8099/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

修改settings.xml 增加服務器賬號密碼信息

<server>
    <id>nexus-release</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>nexus-snapshot</id>
    <username>admin</username>
    <password>admin123</password>
</server>

生成腳手架

  • mvn archetype: create-from-project 從項目生成腳手架
  • cd /target/generated-soource/archetype 轉到此目錄
  • mvn install 發布到倉庫
  • 可以添加到ide的腳手架列表
  • mvn archetype:generate –DarchetypeCatagory=local 命令行方式創建項目 local參數指定走本地倉庫

(二)Java工程化--Maven實踐