大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 Maven插件總結(jié)

Maven插件總結(jié)

更新時間:2021-07-14 16:53:15 來源:動力節(jié)點(diǎn) 瀏覽1733次

Maven工程標(biāo)準(zhǔn)架構(gòu) 

目錄 備注
${basedir} 存放 pom.xml和所有的子目錄
${basedir}/src/main/resources 項(xiàng)目的資源,如spring配置文件,properties資源文件等
${basedir}/src/main/webapps web項(xiàng)目特有
${basedir}/src/test/java 項(xiàng)目的測試類,比如說 JUnit代碼、TestNg代碼
${basedir}/src/test/resources 測試代碼使用的資源

插件一maven-resources-plugin

Maven可以區(qū)別對待Java代碼文件和資源文件,默認(rèn)的主資源文件目錄是src/main/resources,我們可以通過這個插件實(shí)現(xiàn)資源文件過濾。資源文件過濾的意思是指我們可以在資源文件里用使用占位符${propertyName},然后開啟對資源文件的過濾,pom.xml里再統(tǒng)一設(shè)置所有{propertyName}對應(yīng)的值,就可以在構(gòu)建過程中將值替換掉資源文件中對應(yīng)的${propertyName},實(shí)現(xiàn)了代碼配置分離、做到了參數(shù)的統(tǒng)一維護(hù)。

示例用法

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>properties/*.properties</include>
        </includes>
        <filtering>true</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>*.xml</include>
            <include>mapper/**/*.xml</include>
            <include>mysqlMapper/**/*.xml</include>
            <include>*.properties</include>
        </includes>
    </resource>
</resources>
……
<properties>
    <runtime.env>local</runtime.env>
</properties>

我們的主應(yīng)用集成后,會根據(jù)實(shí)際要求部署到不同的環(huán)境中,比如聯(lián)調(diào)環(huán)境、測試環(huán)境、壓力環(huán)境、預(yù)發(fā)布環(huán)境、生產(chǎn)環(huán)境等,而這些環(huán)境上的資源配置信息顯然是不一樣的,針對每套環(huán)境,每個具體占位符${propertyName}都會有不同的值,而這種場景可以使用Maven的profile來支持,每個profile都可以獨(dú)立維護(hù)一套參數(shù)值,在mvn package的時候靈活指定;此外,maven也支持在package的時候指定多個profile,這個特性在執(zhí)行自動部署的時候特別有用。使用這個插件,我們的項(xiàng)目可以做到多環(huán)境支持,參考命令

mvn package -Pnocheck,env-test 

示例用法

<profiles>
    <profile>
        <id>nocheck</id>
        <properties>
            <skipTests>true</skipTests>
            <checkstyle.skip>true</checkstyle.skip>
            <license.skip>true</license.skip>
            <notice.skip>true</notice.skip>
            <versions.skip>true</versions.skip>
        </properties>
    </profile>
    <profile>
        <!-- 本地環(huán)境,默認(rèn)是windows -->
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <runtime.env>local</runtime.env>
        </properties>
    </profile>
    <profile>
        <id>env-test</id>
        <properties>
            <runtime.env>env-test</runtime.env>
        </properties>
    </profile>
</profiles>

插件二maven-jar-plugin

當(dāng)我們將項(xiàng)目模塊化后,有一些通用的資源文件基本上大多數(shù)模塊都會用到,比如log4j.properties,jdbc.properties等,模塊中有了這些資源文件,我們才能單獨(dú)對該模塊進(jìn)行開發(fā)、調(diào)試。默認(rèn)情況下maven-jar-plugin會將這些資源文件全部package成一個jar包進(jìn)行發(fā)布,如果這樣的jar包集成到一個主應(yīng)用中部署,運(yùn)行,很可能導(dǎo)致主應(yīng)用的配置不生效,我稱之為配置混亂,為了解決這個問題,可以開啟maven-jar-plugin的排除功能,在執(zhí)行mvn package之前排除指定的資源文件。

示例用法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>applicationContext.xml</exclude>
            <exclude>properties/**</exclude>
            <exclude>log4j.properties</exclude>
        </excludes>
    </configuration>
</plugin>

插件三maven-war-plugin

項(xiàng)目如果是web主應(yīng)用,我們可以使用maven-war-plugin來對webapps下各類文件進(jìn)行過濾。用法參考maven-resources-plugin

示例用法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <warName>demo-Rest</warName>
        <webResources>
            <resource>
                <directory>src/main/webapp/WEB-INF</directory>
                <filtering>true</filtering>
                <targetPath>WEB-INF</targetPath>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

插件四properties-maven-plugin

隨著項(xiàng)目的不斷迭代,我們的資源配置項(xiàng)將會變得更多,這個會直接影響到pom.xml的體積膨脹;此外,如果項(xiàng)目目標(biāo)部署環(huán)境比較多,pom.xml將會膨脹得更快,更加難以維護(hù)。為了解決這個問題,我們需要將這些配置信息獨(dú)立出來,并按照不同環(huán)境進(jìn)行歸類,使用properties-maven-plugin就會達(dá)到這個效果。

示例用法(將每個環(huán)境的信息放在不同的目錄下,然后在mvn package切換不同的profile實(shí)現(xiàn)去指定目錄讀取配置信息,用讀取到的value去替換資源配置文件的占位符)

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
        <files>
            <file>profiles/${runtime.env}/jdbc.properties</file>
            <file>profiles/${runtime.env}/redis.properties</file>
            <file>profiles/${runtime.env}/batch.properties</file>
            <file>profiles/${runtime.env}/config.properties</file>
        </files>
    </configuration>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
        </execution>
    </executions>
</plugin>

插件五maven-assembly-plugin

Java項(xiàng)目中有一種類型的主應(yīng)用,是需要獨(dú)立部署在后臺啟動的,比如socket服務(wù)程序,比如定時調(diào)度程序,比如dubbo服務(wù)程序,這些程序理論上只需要執(zhí)行一個簡單的java命令即可;稍微復(fù)雜一些的,我們可以規(guī)范一下自己的主應(yīng)用結(jié)構(gòu),定義配置文件夾和依賴庫文件夾,再準(zhǔn)備啟動的批處理腳本sh或bat文件即可。使用maven-assembly-plugin就可以達(dá)到這種效果。

示例用法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
            <descriptor>target/classes/package.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

附package.xml

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>package</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <includes>
                <include>*.sh</include>
                <include>*.bat</include>
            </includes>
            <filtered>true</filtered>
            <outputDirectory></outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>${basedir}/src/main/config</directory>
            <includes>
                <include>*.properties</include>
                <include>log4j.xml</include>
            </includes>
            <outputDirectory>config</outputDirectory>
            <filtered>true</filtered>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>${basedir}/src/main/config</directory>
            <includes>
                <include>log4j.dtd</include>
            </includes>
            <outputDirectory>config</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <fileMode>0644</fileMode>
        </dependencySet>
    </dependencySets>
</assembly>

附示例生成的Java應(yīng)用結(jié)構(gòu)圖

maven插件

插件六maven-shade-plugin

有時候,我們需要將所有配置文件和依賴庫文件全部放在一個jar包中,運(yùn)維的同事只需要執(zhí)行java-jar batch.jar即可完成啟動。雖然使用maven-assembly-plugin也可以做到這一點(diǎn),但是在讀取配置文件的時候有可能會遇到一些問題,這個時候,我們可能需要使用到maven-shade-plugin這個插件,經(jīng)筆者實(shí)踐按照如下示例用法配置確實(shí)可用;當(dāng)然本示例配置了mainClass,直接執(zhí)行java-jar batch.jar確實(shí)沒問題,但如果執(zhí)行java com.fastjrun.demospring4.BatchInit-classpath batch.jar也是可以的。

示例用法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <finalName>batch</finalName>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.fastjrun.demospring4.BatchInit</mainClass>                    </transformer>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.tooling</resource>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

插件七versions-maven-plugin

當(dāng)項(xiàng)目模塊化后,我們會遇到一個問題,就是項(xiàng)目版本升級的時候,需要同時變更父模塊和所有子模塊中的版本號,而這是一個比較瑣碎且容易出錯的事情,還好maven考慮得很周到,提供了這樣一個插件,我們使用命令行就可以達(dá)到效果了。我們的項(xiàng)目視圖如下

maven插件

參考命令如下

mvn versions:set -DnewVersion=1.2-SNAPSHOT

總結(jié)

本文匯總了常用的幾個插件及其用法,經(jīng)實(shí)踐,基于eclipse的kepler、luna版本都能很好支持maven-resources-plugin、maven-jar-plugin、maven-war-plugin和properties-maven-plugin使用,同時也支持profile的activeByDefault設(shè)置,研發(fā)同事在不需要任何調(diào)整的情況下就能直接開發(fā)、調(diào)試代碼,且在開發(fā)結(jié)束后,可以直接使用mvn命令打包,打出各個環(huán)境的部署程序。從開發(fā)、調(diào)試、測試、驗(yàn)證到上線的整個過程,所有模塊的pom.xml直到下一個迭代變更版本前都不用修改,直接使用。

以上就是動力節(jié)點(diǎn)小編介紹的"Maven插件總結(jié)",希望對大家有幫助,想了解更多可查看Maven教程。動力節(jié)點(diǎn)在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識,讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 久热中文字幕在线 | 日韩高清在线日韩大片观看网址 | 特黄特色大片免费播放器999 | 国产精品久久国产三级国不卡顿 | 亚洲美女精品视频 | 中文字幕亚洲无线码在线一区 | 国产精品探花一区在线观看 | 久久精品午夜视频 | 亚洲免费视 | 国产小视频免费观看 | 四虎国产精品免费观看 | 一级特黄特黄的大片免费 | 亚洲在线视频免费观看 | 久久天天躁夜夜躁狠狠 | 九九99香蕉在线视频网站 | 久久久久亚洲精品美女 | 91成人精品 | 国产精选一区二区 | 99网| 免费性生活视频 | 一级毛片全部免费播放 | 免费毛片a | 日韩精品成人在线 | 国产18到20岁美女毛片 | 久久在线免费视频 | 欧美一区在线观看视频 | 香蕉久久ac一区二区三区 | 麻豆精品在线 | 午夜一级成人 | 国产三级做爰在线观看∵ | 国产精品视频免费视频 | 欧美特级毛片 | 成年女人免费看片 | 久久免费视频在线观看 | 伊人96| 久久色成人 | 亚洲国产精品乱码一区二区三区 | 欧美 亚洲 另类 热图 | 欧美性生活一级 | 亚洲福利一区二区三区 | 亚欧美视频 |