5

I am trying to use maven-assembly-plugin to build a lite and fat web application, the application will have different content later. I thought that I actually can use two profiles mvn -Pliteweb,fatweb package so it will create two build assembly for each profile. But when I run it, it actually only create one assembly that is in the bottom position in the pom ( the liteweb )

I already tried when I build it one by one its okay. I also check with mvn help:active-profiles -P fatweb,liteweb and it correctly show 2 active profile.

Below is my test pom ( its not including the difference in here, I just want it to create 2 War files and other assembly files separately ). I am still new at Maven so I might misunderstood this. Is creating multiple assembly from multiple profiles is possible to do?

<?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 for now I didnt put anything just to make sure everything work.

<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 same I didnt put anything to make sure everything work, but I already test with different thing inside still doesnt work.

<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>

When EXECUTING 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)

And below is what happen when I execute mvn -Pfatweb,liteweb clean package seems like it building the same zip twice.. From same xml assembly, but actually from different execution ( exec1 and 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] ------------------------------------------------------------------------

So the profile actually active, but after that the one that build in target folder is only the LITEWEB. If anyone know, please help me to understand why it not create both and why mvn like build the zip twice. I know the workaround is just shellscript code to build twice (if I build one profile each time it work correctly), but I want to use only mvn specific build.

Below is what happen if I do mvn -Pfatweb,liteweb clean install the lib were created twice, but it only build the WAR file once. From the log I actually realized the one that build the WAR is the war-plugin, but how do I make it execute for both profile..?

[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] ------------------------------------------------------------------------ 
leafy
  • 156
  • 1
  • 8
  • `assembly` is deprecated, you should use `single`. But also, you should bind your `maven-assembly-plugin` to a phase, like `package`. It won't be invoked otherwise. – Tunaki Jun 13 '16 at 08:56
  • @Tunaki I actually used `package` at first but somehow it looks like it build the WAR and the Descriptor twice, the result is one, but from the log it executed twice. Anyway I used the package phase for now. For the single, I already tried and still the same its not correctly create both WAR – leafy Jun 13 '16 at 09:39
  • Okay I just realized, if I call `mvn -Pliteweb package` only, it only executed once at maven log. (using phase `package` and `single` goal ) So looks like that the goal called twice but somehow not the correct one ( same goal executed twice).. – leafy Jun 13 '16 at 09:44
  • finalName only influences the name of the file in the target folder, not in the maven repository. In your definition the second execution overwrites the first (At least in the maven repo) – mediahype Jun 13 '16 at 10:24
  • @mediahype How to create it so it wont overwrite? The WAR that I checked is in target folder. (Only one created) – leafy Jun 13 '16 at 10:37
  • How does your assembly descriptor look like apart from that can you show your full pom file? – khmarbaise Jun 13 '16 at 12:19
  • ` assembly.xml ` inside it just some exclude, for the lite web its just different file and different exclude. I dont think its a problem since if I build it one by one its okay. – leafy Jun 13 '16 at 13:09
  • Are there two different `assembly.xml` descriptors (e.g. assembly-liteweb.xml and assembly-fatweb.xml)? Do the two assembly files have different ``s? – user944849 Jun 13 '16 at 18:43
  • Also consider using a later version of the plugin. The POM above shows version 2.2.1 while the latest is [2.6](http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html). – user944849 Jun 13 '16 at 18:46
  • Yes, the id in assemble is different and in different files, and I already tried 2.6 too. But still the same it only build once. – leafy Jun 14 '16 at 06:36
  • Updated the questions with more details and the thing I did. – leafy Jun 14 '16 at 07:21

1 Answers1

4

In your POM move your <configuration> section FROM being under <plugin> TO being under <execution>. Do this for both <configuration> sections.

The error is that you configured the plugin 2 times. When both profiles are active, maven merges the 2 configurations, loosing one.

mediahype
  • 480
  • 2
  • 8
  • Ah you were right, now it take from both configuration files correctly. But the naming still used the last one and it didnt build the WAR file.. Is it the same issue? Maven look the plugin as same one so it only create 1 WAR? Is it possible so it can create each WAR file and lib.zip following the final name? (Like when I build it one by one) – leafy Jun 15 '16 at 02:23
  • Do not look into your target folder. Final name only influences the file name in your target folder. The truth lies in your local repository (install) or remote repository (deploy). That's the file name (coordinates) by which other people reference your artifact. There should be 2 different files because you gave 2 different id in your 2 assembly.xml – mediahype Jun 15 '16 at 06:47
  • even if in the local folder (.m2 repositories ...) it only create one war file.. And actually I always deploy the one in target folder not in the maven repositories. Is this not correct? – leafy Jun 15 '16 at 09:02
  • Strange. Please post your output when running clean install. The part where the install plugin is running – mediahype Jun 15 '16 at 12:21
  • So the assembly (lib zip) already ok, just the WAR that I need to create for both. Updated with the log of clean install. – leafy Jun 16 '16 at 02:56
  • You did not mention that you want 2 war. Why ? How should they be different ? – mediahype Jun 16 '16 at 13:04
  • Yes I mention it at my question actually. Sorry if I am a bit misleading. The fat one is the actual at the master server, and the lite will be used for demo at the slave later on. I need it because the demo will be used and removed a lot so I need it for faster performance. The main difference of course there is a lot of function removed/added. – leafy Jun 17 '16 at 02:29
  • Wow. Although you could get this to work (with assembly), I do not think it is a good idea. You need 2 different maven war modules. Google for "maven war plugin" and read about Overlays. They could be your friend – mediahype Jun 20 '16 at 18:25