+ (点击以下标题显示正文内容)
给war包添加静态资源路径(配置多个web静态资源路径)
maven-war-plugin:webResources.resource.directory
可以配置多个 resource,每个resource都可以配置 排除或包含文件(includes 和 excludes)。
如下例所示:
<!-- 打war包 --> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <executions> <execution> <id>default-war</id> <phase>package</phase> <goals> <goal>war</goal> </goals> </execution> </executions> <configuration> <!-- 把war里面的class当成附件打成一个jar包 --> <attachClasses>true</attachClasses> <webResources> <!-- 资源1:包含对应静态资源文件,默认会拷贝到web root目录下 --> <resource> <directory>D:/C/dev-solft/tomcat-7.0/zoa</directory> </resource> <!-- 资源2:包含对应静态资源文件 --> <resource> <directory>${basedir}/ext-resources/local</directory> <includes> <include>**/*.jpg</include> <include>**/*.vm</include> </includes> <excludes> <exclude>*pattern3/pattern3</exclude> <exclude>pattern4/pattern4</exclude> </excludes> <!--By default web resources are copied to the root of the WAR --> <!-- override the destination directory for this resource --> <targetPath>WEB-INF</targetPath> </resource> </webResources> </configuration> </plugin>
具体参见:
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
+ (点击以下标题显示正文内容)
配置多个resource路径
批量引入本地lib目录下的jar包或任何资源
方法应该不止一种,下面介绍这种方法比较方便,在pom.xml里面配置即可:
build:resources.resource. directory / targetPath
<build> <finalName>${release-name}</finalName> <sourceDirectory>${basedir}/src/main/java</sourceDirectory> <outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory> <resources> <resource> <directory>${basedir}/src/main/resources</directory> <excludes> <exclude>*.copyarea.db</exclude> </excludes> </resource> <resource> <directory>${basedir}/lib</directory> <targetPath>${basedir}/src/main/webapp/WEB-INF/lib</targetPath> </resource> </resources>
+ (点击以下标题显示正文内容)
Maven打war包时排除不想打包的静态资源文件
warSourceExcludes
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <!-- 把war里面的class当成附件打成一个jar包 --> <attachClasses>true</attachClasses> <warSourceExcludes>**/.copyarea.db</warSourceExcludes> </configuration> </plugin>
注意,和以下两个的区别:
<dependentWarExcludes> 和 <webResources>
具体可以查看官方手册。
+ (点击以下标题显示正文内容)
Maven打war包时,不导入jar文件(lib下为空)、排除指定jar包
maven-war-plugin:packagingExcludes
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <!-- 把war里面的class当成附件打成一个jar包 --> <attachClasses>true</attachClasses> <warSourceExcludes>**/.copyarea.db</warSourceExcludes> <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> </configuration> </plugin>
+ (点击以下标题显示正文内容)
打包源代码时排除某些文件
maven-source-plugin:configuration.excludes
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> <configuration> <excludes> <exclude>**/.copyarea.db</exclude> <exclude>**/*.keep</exclude> <exclude>**/*.bak</exclude> </excludes> </configuration> </execution> </executions> </plugin>
+ (点击以下标题显示正文内容)
将依赖打入source源码包
maven-source-plugin:execution.<goal>aggregate</goal>
<plugin> <artifactId>maven-source-plugin</artifactId> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>package</phase> <goals> <!-- 将依赖打入source包 --> <goal>aggregate</goal> </goals> </execution> </executions> </plugin>
+ (点击以下标题显示正文内容)
合并打包:将所有依赖的jar包打包在一起、打包成一个jar包
方法一:maven-shade-plugin(推荐)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>ALL</shadedClassifierName> </configuration> </execution> </executions> </plugin>
方法二:maven-assembly-plugin
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.zollty.maven.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
例2:
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.zollty.util.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
运行下面的指令:
assembly:assembly
+ (点击以下标题显示正文内容)
强制排除Jar包依赖(包括传递的依赖)
用maven-enforcer-plugin检查,当发现有依赖的jar包则构建失败
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <executions> <!-- <execution> <id>enforce-versions</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireMavenVersion> <version>2.1.0</version> </requireMavenVersion> <requireJavaVersion> <version>1.6</version> </requireJavaVersion> </rules> </configuration> </execution> --> <execution> <id>enforce-banned-dependencies</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <bannedDependencies> <excludes> <!-- <exclude>junit:junit</exclude> --> <exclude>org.slf4j:slf4j-api</exclude> <exclude>commons-logging:commons-logging</exclude> </excludes> <!-- <includes> <include>junit:junit:4.8.2:jar:test</include> <include>cglib:cglib-nodep:jar:2.2</include> </includes> --> </bannedDependencies> </rules> <fail>true</fail> </configuration> </execution> </executions> </plugin>