2016-06-13 77 views
3

我正在尝试使用maven-assembly-plugin来构建一个轻量级和胖的web应用程序,之后应用程序将拥有不同的内容。我认为我实际上可以使用两个配置文件mvn -Pliteweb,fatweb package,因此它会为每个配置文件创建两个构建配件。但是当我运行它时,它实际上只创建了一个位于pom(liteweb)底部位置的程序集。我已经尝试过,当我逐一构建它时,它就没有问题。我也检查mvn help:active-profiles -P fatweb,liteweb,它正确显示2活动配置文件。Maven maven-assembly-plugin使用配置文件构建

下面是我的测试pom(它不包括这里的区别,我只是希望它分别创建2个War文件和其他程序集文件)。我对Maven仍然很陌生,所以我可能会误解这一点。从多个配置文件创建多个程序集是可能的吗?

<?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/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 
<groupId>com.test.web</groupId> 
<artifactId>TEST_WEB</artifactId> 
<packaging>war</packaging> 
<name>WEB Application</name> 
<version>0.0.1</version> 

<properties> 
    <litewebPath>src/main/lite</litewebPath> 
    <fatwebPath>src/main</fatwebPath> 
</properties> 

<profiles> 
    <profile> 
     <id>fatweb</id> 
     <build> 
      <resources> 
       <resource> 
        <directory>${fatwebPath}/resources</directory> 
        <filtering>true</filtering> 
       </resource> 
      </resources> 
      <finalName>WEB-${project.version}</finalName> 
      <plugins> 
       <plugin> 
        <artifactId>maven-assembly-plugin</artifactId> 
        <version>2.6</version> 
        <configuration> 
         <descriptors> 
          <descriptor>fatassembly.xml</descriptor> 
         </descriptors> 
        </configuration> 
        <executions> 
         <execution> 
          <id>exec1</id> 
          <phase>package</phase> 
          <goals> 
           <goal>single</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

      </plugins> 
     </build> 
    </profile> 
    <profile> 
     <id>liteweb</id> 
     <build> 
      <resources> 
       <resource> 
        <directory>${litewebPath}/resources</directory> 
        <filtering>true</filtering> 
       </resource> 
      </resources> 
      <finalName>LITEWEB-${project.version}</finalName> 
      <plugins> 
       <plugin> 
        <artifactId>maven-assembly-plugin</artifactId> 
        <version>2.6</version> 
        <configuration> 
         <descriptors> 
          <descriptor>liteassembly.xml</descriptor> 
         </descriptors> 
        </configuration> 
        <executions> 
         <execution> 
          <id>exec2</id> 
          <phase>package</phase> 
          <goals> 
           <goal>single</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

fatassembly.xml现在我没有把任何东西只是为了确保一切工作。

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> 

    <id>lib</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <dependencySets> 
     <dependencySet> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

liteassembly.xml相同我没有把任何东西,以确保一切工作,但我已经测试与不同的东西里面仍然不工作。

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> 

    <id>lib-lite</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <dependencySets> 
     <dependencySet> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

当执行mvn help:active-profiles -Pfatweb,liteweb

Active Profiles for Project 'com.test.web:TEST_WEB:war:0.0.1': 

The following profiles are active: 

- fatweb (source: com.test.web:TEST_WEB:0.0.1) 
- liteweb (source: com.test.web:TEST_WEB:0.0.1) 

并在下面当我执行MVN -Pfatweb,liteweb清洁套装好像它建同一个zip两次发生什么。从相同的XML组件,但实际上从不同的执行(exec1和exec2)

[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building WEB Application 0.0.1 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TEST_WEB --- 
[INFO] Deleting D:\PROJECT\POMTEST\target 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TEST_WEB --- 
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] Copying 0 resource 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TEST_WEB --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TEST_WEB --- 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory D:\PROJECT\POMTEST\src\test\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TEST_WEB --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TEST_WEB --- 
[INFO] No tests to run. 
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ TEST_WEB --- 
[INFO] Packaging webapp 
[INFO] Assembling webapp [TEST_WEB] in [D:\PROJECT\POMTEST\target\LITEWEB-0.0.1] 
[INFO] Processing war project 
[INFO] Copying webapp resources [D:\PROJECT\POMTEST\src\main\webapp] 
[INFO] Webapp assembled in [27 msecs] 
[INFO] Building war: D:\PROJECT\POMTEST\target\LITEWEB-0.0.1.war 
[INFO] WEB-INF\web.xml already added, skipping 
[INFO] 
[INFO] --- maven-assembly-plugin:2.6:single (exec1) @ TEST_WEB --- 
[INFO] Reading assembly descriptor: liteassembly.xml 
[INFO] Building zip: D:\PROJECT\POMTEST\target\LITEWEB-0.0.1-lib-lite.zip 
[INFO] 
[INFO] --- maven-assembly-plugin:2.6:single (exec2) @ TEST_WEB --- 
[INFO] Reading assembly descriptor: liteassembly.xml 
[INFO] Building zip: D:\PROJECT\POMTEST\target\LITEWEB-0.0.1-lib-lite.zip 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 1.961 s 
[INFO] Finished at: 2016-06-14T15:23:05+08:00 
[INFO] Final Memory: 12M/229M 
[INFO] ------------------------------------------------------------------------ 

所以个人资料确实活跃,但之后即构建目标文件夹中的一个只有LITEWEB。如果有人知道,请帮我理解为什么它不会创建两个,为什么mvn像构建zip两次。我知道解决方法只是构建两次的shell脚本代码(如果每次构建正确时都构建一个配置文件),但我只想使用mvn特定构建。

下面是如果我做mvn -Pfatweb,liteweb clean install这个库创建两次,但它只构建WAR文件一次。从日志中我实际上意识到构建WAR的是战争插件,但是如何让它执行两个配置文件..?

[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building WEB Application 0.0.1 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TEST_WEB --- 
[INFO] Deleting D:\PROJECT\POMTEST\target 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TEST_WEB --- 
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] Copying 0 resource 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TEST_WEB --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TEST_WEB --- 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory D:\PROJECT\POMTEST\src\test\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TEST_WEB --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TEST_WEB --- 
[INFO] No tests to run. 
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ TEST_WEB --- 
[INFO] Packaging webapp 
[INFO] Assembling webapp [TEST_WEB] in [D:\PROJECT\POMTEST\target\LITEWEB-0.0.1] 
[INFO] Processing war project 
[INFO] Copying webapp resources [D:\PROJECT\POMTEST\src\main\webapp] 
[INFO] Webapp assembled in [26 msecs] 
[INFO] Building war: D:\PROJECT\POMTEST\target\LITEWEB-0.0.1.war 
[INFO] WEB-INF\web.xml already added, skipping 
[INFO] 
[INFO] --- maven-assembly-plugin:2.6:single (exec1) @ TEST_WEB --- 
[INFO] Reading assembly descriptor: fatassembly.xml 
[INFO] Building zip: D:\PROJECT\POMTEST\target\LITEWEB-0.0.1-lib.zip 
[INFO] 
[INFO] --- maven-assembly-plugin:2.6:single (exec2) @ TEST_WEB --- 
[INFO] Reading assembly descriptor: liteassembly.xml 
[INFO] Building zip: D:\PROJECT\POMTEST\target\LITEWEB-0.0.1-lib-lite.zip 
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ TEST_WEB --- 
[INFO] Installing D:\PROJECT\POMTEST\target\LITEWEB-0.0.1.war to C:\Users\rm\.m2\repository\com\test\web\TEST_WEB\0.0.1\TEST_WEB-0.0.1.war 
[INFO] Installing D:\PROJECT\POMTEST\pom.xml to C:\Users\rm\.m2\repository\com\test\web\TEST_WEB\0.0.1\TEST_WEB-0.0.1.pom 
[INFO] Installing D:\PROJECT\POMTEST\target\LITEWEB-0.0.1-lib.zip to C:\Users\rm\.m2\repository\com\test\web\TEST_WEB\0.0.1\TEST_WEB-0.0.1-lib.zip 
[INFO] Installing D:\PROJECT\POMTEST\target\LITEWEB-0.0.1-lib-lite.zip to C:\Users\rm\.m2\repository\com\test\web\TEST_WEB\0.0.1\TEST_WEB-0.0.1-lib-lite.zip 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 6.768 s 
[INFO] Finished at: 2016-06-16T10:49:10+08:00 
[INFO] Final Memory: 12M/174M 
[INFO] ------------------------------------------------------------------------ 
+0

'程序集'已弃用,您应该使用'single'。但是,你也应该将'maven-assembly-plugin'绑定到一个阶段,比如'package'。它不会被调用。 – Tunaki

+0

@Tunaki我实际上首先使用了'包',但不知何故它看起来像构建了WAR和描述符两次,结果是一个,但是从日志中它执行了两次。 无论如何,我现在使用了包装阶段。 对于单机,我已经尝试过,仍然是相同的,它不能正确创建两个WAR – leafy

+0

好吧,我只是意识到,如果我只调用'mvn -Pliteweb软件包',它只会在maven日志中执行一次。 (使用阶段'package'和'single'目标)所以看起来目标被调用了两次,但不知何故不是正确的(相同的目标执行了两次)。 – leafy

回答

1

在你的POM移动你的<configuration>部分FROM<plugin><execution>下。对<configuration>这两个部分执行此操作。

错误在于您配置了插件2次。当两个配置文件都处于活动状态时,maven合并这两个配置,丢失一个配置文件。

+0

你是对的,现在它正确地从两个配置文件中取得。但命名仍然使用最后一个,它没有建立WAR文件。它是同样的问题? Maven将插件看作同一个插件,所以它只能创建1个WAR?是否有可能在最终名称后面创建每个WAR文件和lib.zip? (就像我一个接一个地构建它一样) – leafy

+0

不要看着你的目标文件夹。最终名称仅影响目标文件夹中的文件名。事实在于您的本地存储库(安装)或远程存储库(部署)。这是其他人引用您的工件的文件名(坐标)。应该有2个不同的文件,因为即使在本地文件夹(.m2存储库...)中,您在2个assembly.xml中提供了2个不同的ID, – mediahype

+0

也只会创建一个war文件。实际上,我总是部署一个目标文件文件夹不在Maven仓库中。这是不正确的? – leafy