maven 通過profiles管理不同環境的依賴和插件

Profile能讓你爲一個特殊的環境自定義一個特殊的構建;profile使得不同環境間構建的可移植性成爲可能。Maven中的profile是一組可選的配置,可以用來設置或者覆蓋配置默認值。有了profile,你就可以爲不同的環境定製構建。profile可以在pom.xml中配置,並給定一個id。然後你就可以在運行Maven的時候使用的命令行標記告訴Maven運行特定profile中的目標。一個Profiles下面允許出現的元素:

<project>
    <profiles>
        <profile>
            <build>
                <defaultGoal>...</defaultGoal>
                <finalName>...</finalName>
                <resources>...</resources>
                <testResources>...</testResources>
                <plugins>...</plugins>
            </build>
            <reporting>...</reporting>
            <modules>...</modules>
            <dependencies>...</dependencies>
            <dependencyManagement>...</dependencyManagement>
            <distributionManagement>...</distributionManagement>
            <repositories>...</repositories>
            <pluginRepositories>...</pluginRepositories>
            <properties>...</properties>
        </profile>
    </profiles>
</project>  

profile的激活方式

Maven提供了多種不同的profile激活方式。比如可以使用mvn命令的-P參數顯示的激活一個profile,也可以根據環境條件的設置讓它自動激活等。

使用activeByDefault設置激活

<profiles>  
    <profile> 
        <id>gray</id> 
        <properties> 
            <cn.springcloud.gray.version>A.1.1.0.RELEASE</cn.springcloud.gray.version> 
        </properties> 
		<activation> 
            <activeByDefault>true</activeByDefault> 
        </activation> 
    </profile> 
</profiles> 

在settings.xml中使用activeProfiles指定處於激活狀態的profile

在settings.xml中使用activeProfiles來指定需要激活的profile,這種方式激活的profile將所有情況下都處於激活狀態

<profiles>  
    <profile> 
        <id>gray</id> 
        <properties> 
            <cn.springcloud.gray.version>A.1.1.0.RELEASE</cn.springcloud.gray.version> 
        </properties>  
    </profile> 
</profiles> 
<activeProfiles> 
    <activeProfile>profileTest1</activeProfile> 
</activeProfiles> 

使用-P參數顯示的激活

在進行Maven操作時就可以使用-P參數顯示的指定當前激活的profile。可以指定多個profile,profile之間用逗號隔開

mvn clean install -Pgray
mvn clean install -P gray
mvn clean install -Pgray,test

使用-P參數顯示的不激活

當項目使用activeByDefault或settings.xml中激活的profile,但是在某些場景下又不想它處於激活狀態。比如不想做讓gray處於激活狀態,這個時候可以這樣做

mvn clean install -P !gray

查看當前處於激活狀態的profile

Maven提供了一個指令可以查看當前處於激活狀態的profile,查看命令是

mvn help:active-profiles

不同環境依賴不同的版本或jar包、插件

在項目的開發過程中,可能會有這樣的需求,生產環境依賴的jar包必須是release版本, 測試環境可以是snapshot版本。這個時候就可以使用profile去管理不同環境的依賴。如下:

<project ... >
	<properties>
		<springcloud-gray.version>1.0.0.RELEASE</springcloud-gray.version>
	</properties>
	<dependencies>
		<dependency>
    		<groupId>cn.springcloud.gray</groupId>
    		<artifactId>spring-cloud-starter-gray-client</artifactId>
    		<version>${springcloud-gray.version}</version>
		</dependency>
	</dependencies>
	<profiles>
    	<profile>
        	<id>test</id>
        	<properties>
            	<zma-stu-springcloud-gray.version>1.1.0-SNAPSHOT</zma-stu-springcloud-gray.version>
        	</properties>
			<plugins>
    			<plugin>
        			<groupId>org.apache.maven.plugins</groupId>
        			<artifactId>maven-source-plugin</artifactId>
        			<version>2.2.1</version>
        			<executions>
            			<execution>
                			<phase>package</phase>
                			<goals>
                    			<goal>jar-no-fork</goal>
                			</goals>
            			</execution>
        			</executions>
    			</plugin>
			</plugins>
    	</profile>
	</profiles>
</project>

idea上管理maven profiles

在idea上對maven的profiles進行管理是非常簡單的。
在maven面板中,Maven -> Profiles

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章