maven-war-plugin根据环境打包资源

声明:本文参考资料来自互联网


J2EE项目在开发时,需要根据不同环境适配资源文件,maven提供了war-plugin这款打包插件,方便完成这个适配。


首先配置maven profile,再次不再详述,参见官方文档。demo中,prod的资源依旧放在src/main/resources中,dev、qa资源置于src目录外。可根据实际情况调整。

<profiles>
		<!-- dev配置 -->
		<profile>
			<id>dev</id>
			<activation>
        		<activeByDefault>true</activeByDefault>
    		</activation>
			<properties>
				<runtime.env>env/dev</runtime.env>
				<final.name>filename</final.name>
			</properties>
		</profile>
		<!-- qa配置 -->
		<profile>
			<id>qa</id>
			<properties>
				<runtime.env>env/qa</runtime.env>
				<final.name>filename</final.name>
			</properties>
		</profile>
		<!-- prod配置 -->
		<profile>
			<id>prod</id>
			<properties>
				<runtime.env>src/main/resources</runtime.env>
				<final.name>filename</final.name>
			</properties>
		</profile>
	</profiles>

然后引入maven-war-plugin插件

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.0.0</version>
				<configuration>
					<webResources>
						<resource>
							<directory>${runtime.env}</directory>
							<targetPath>WEB-INF/classes</targetPath>
						</resource>
					</webResources>
					<warSourceExcludes>resources/js/**</warSourceExcludes>
				</configuration>
			</plugin>


 
 

warSourceExcludes 可以过滤掉不需要打包的资源文件


--- 20161220 补充 yuicompressor-maven-plugin 插件,打包js,css成min文件 ---

<plugin>
				<groupId>net.alchim31.maven</groupId>
				<artifactId>yuicompressor-maven-plugin</artifactId>
				<version>1.3.0</version>
				<executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>compress</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                	<encoding>UTF-8</encoding>
                	<jswarn>false</jswarn>
                	<force>false</force>
                	<suffix>.min</suffix>
                	
                	<excludes>
                        <exclude>**/*.min.js</exclude>
                        <exclude>**/*.min.css</exclude>
                        <exclude>resources/component/**</exclude>
                        <exclude>resources/image/**</exclude>
                    </excludes>
                    
                    <includes>  
                		<include>**/*.js</include>  
                		<include>**/*.css</include>  
            		</includes>
                </configuration>
			</plugin>


猜你喜欢

转载自blog.csdn.net/u013275741/article/details/48622783
今日推荐