点击数:22
Scala打包war包
war包即对WEB应用程序进行打包,用于应用容器的部署,比如tomcat,netty等。编译Scala需要引入scala-maven-plugin插件,编译java需要引入maven-compiler-plugin插件。大多数的时候我们的项目是scala和java的混编,所以,编译的时候需要这两个插件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-target:jvm-1.8</arg>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
maven默认打包是jar包,<packaging>war</packaging>
。此外,我们需要引入另外一个插件:maven-war-plugin,该插件可以把一个项目打包为war包。默认情况下,maven-war-plugin需要打包的是一个web项目,如果你的项目不是web项目,是不能正常打包的。可能出现的错误:
– Execution default-war of goal org.apache.maven.plugins:maven-war-plugin:3.1.0:war failed: basedir /Users/${user}/Documents/${project}/src/main/webapp/WEB-INF does not exist -> [Help 1]
Error assembling WAR: webxml attribute is required
第一个错误原因是没有webapp/WEB-INF文件夹。
第二个错误是没有web.xml文件。
综上,也就是需要是一个web项目才能打包。那是不是不是web项目不能打包呢,正如你所期望的,不是web项目也是可以打包的。需要配置maven-war-plugin插件的configuration元素。configuration中有一个failOnMissingWebXml元素,设置其为false即可。如果要某些文件不打包到war包中,可以使用configuration元素的warSourceExcludes元素指定即可。
web项目打包可用的配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<webResources>
<resource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>src/main/webapp/WEB-INF</directory>
<!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
<filtering>true</filtering>
<!-- 目标路径 -->
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
非web项目打包war包的配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
Maven构建命令:
mvn clean scala:compile compile package
下面是配置属性相对较全面的maven-war-plugin配置,但不是所必须的。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>
**/spy.properties,scripts/ext4/docs/**,scripts/ext4/examples/**,scripts/ext4/pkgs/**,scripts/ext4/src/**,scripts/ext4/welcome/**,
scripts/jquery-ui/development-bundle/**,scripts/jquery-ui/extra/**,
scripts/jqgrid/src/**,scripts/jqgrid/plugins/**,
scripts/colorbox/example*/**,
scripts/jquery-fancybox/example/**,
scripts/masonry/doc/**,
scripts/poshytip/demo/**,
scripts/zTree/demo/**,
logs/**
</packagingExcludes>
<warSourceExcludes>
scripts/ext4/docs/**,scripts/ext4/examples/**,scripts/ext4/pkgs/**,scripts/ext4/src/**,scripts/ext4/welcome/**,
scripts/jquery-ui/development-bundle/**,scripts/jquery-ui/extra/**,
scripts/jqgrid/src/**,scripts/jqgrid/plugins/**,
scripts/colorbox/example*/**,
scripts/jquery-fancybox/example/**,
scripts/masonry/doc/**,
scripts/poshytip/demo/**,
scripts/zTree/demo/**,
logs/**
</warSourceExcludes>
<warSourceDirectory>WebContent</warSourceDirectory>
<workDirectory>${project.build.directory}/work</workDirectory>
<useCache>false</useCache>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
</archive>
<webXml>${basedir}/WebContent/WEB-INF/web.xml</webXml>
<ignoreWebxml>false</ignoreWebxml>
<warName>${project.build.finalName}</warName>
<finalName>${project.build.finalName}</finalName>
<webResources>
<resource>
<directory>WebContent/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
Maven打包war的两种方式
- 第一种maven-war-plugin
第一种的打包如上所示:[scala打包war包]()
-
第二种方式:maven-dependency-plugin 、maven-antrun-plugin 、 build-helper-maven-plugin
maven-dependency-plugin :相当于数据收集器,该依赖那些jar包,该加载那些resource信息。
maven-antrun-plugin:完成收集完文件夹的压缩和包的重命名。
build-helper-maven-plugin:让压缩包拥有war本色。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<includeScope>compile</includeScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
其他人的说法:如果想要打包成war包,上面的这些配置结构了。但是为了war拥有war包的本色,需要添加build-helper-maven-plugin的配置。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${pom.artifactId}.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
但是我加了此段配置,结果编译时报错:
Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.4:attach-artifact (attach-artifacts) on project Gatling: Execution attach-artifacts of goal org.codehaus.mojo:build-helper-maven-plugin:1.4:attach-artifact failed: For artifact {com.idongjia.test:Gatling:1.0-SNAPSHOT:war}: An attached artifact must have a different ID than its corresponding main artifact. -> [Help 1]
把type修改为其他格式就好了。所以,是否能起到让war包拥有war包的本色,看来还得自己去琢磨了。修改file和type,就算正确了,war包的名字后缀也没有改变,所以,我觉得起不到如上所说。倒是觉得这篇文章对该插件的描述很到位:[ build-helper-maven-plugin插件](), 同时,为了求证,阅读了其他博文:[Maven插件之build-helper-maven-plugin](), [使用maven插件构建项目安装包](), [build-helper-maven-plugin]()
maven-dependency-plugin是处理与依赖相关的插件。它有很多可用的goal,大部分是和依赖构建、分析和解决相关的goal,这部分goal可以直接用maven的命令操作,例如:mvn dependency:tree、mvn dependency:analyze;这类操作在平时的maven应用中很少会用到。这里主要介绍除此之外的、用得最多的几个操作:copy, copy-dependencies和它们对应的unpack, unpack-dependencies,详细查看这篇文章:[maven-dependency-plugin插件的使用]()
ant是一个老牌的项目打包管理系统了,目前虽然已经慢慢被maven取代,但其功能的强大仍然是很多场合下的首选,尤其是众多的task可以基本满足任何需求。其实在maven中也有使用ant的需求,比如不同环境打包编译时使用不同的配置信息等,或者是说做一些文件删除、复制之类的事情,这有些是maven做不来的,而ant就可以了,况且maven中已经有了maven-antrun-plugin插件,专门为在maven中运行ant做好了准备。详细查看此文:[maven-antrun-plugin(运行ant的插件)](), [Maven实战之antrun插件](), [使用maven打war包过程中对文件进行copy、rename(move)、delete操作](), [maven生成war包的两种方式]()
总结
相对完整的maven-war-plugin配置项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.idongjia.test</groupId>
<artifactId>Gatling</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<scala.version>2.11.7</scala.version>
<encoding>UTF-8</encoding>
<gatling.version>2.1.7</gatling.version>
<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/main/scala</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-target:jvm-1.8</arg>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<webResources>
<resource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>src/main/webapp/WEB-INF</directory>
<!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
<filtering>true</filtering>
<!-- 目标路径 -->
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
相对完整的maven-dependency-plugin 、maven-antrun-plugin** 、 build-helper-maven-plugin配置项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.idongjia.test</groupId>
<artifactId>Gatling</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<scala.version>2.11.7</scala.version>
<encoding>UTF-8</encoding>
<gatling.version>2.1.7</gatling.version>
<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/main/scala</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-target:jvm-1.8</arg>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<!--<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>-->
</execution>
</executions>
<configuration>
<includeScope>compile</includeScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${pom.artifactId}.war</file>
<type>zip</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>