Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven. How to include specific folder or file when assemblying project depending on is it dev build or production?

Using maven-assembly-plugin

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
 <descriptors>
  <descriptor>descriptor.xml</descriptor>
 </descriptors>
 <finalName>xxx-impl-${pom.version}</finalName>
 <outputDirectory>target/assembly</outputDirectory>
 <workDirectory>target/assembly/work</workDirectory>
</configuration>

in descriptor.xml file we can specify

    <fileSets>
    <fileSet>
        <directory>src/install</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

Is it possible to include specific file from this folder or sub-folder depending on profile? Or some other way...

Like this:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/install/dev</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>prod</id>
        <build>
            <resources>
                <resource>
                    <directory>src/install/prod</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

But it puts resources in jar when packaging. But we need to put it in zip when assemblying as I already mentioned above :( Thanks!

like image 279
whatswrong Avatar asked Jan 05 '11 09:01

whatswrong


People also ask

What is fileset in Maven?

Defines the rules for matching and working with files in a given base directory.

What is assembly descriptor in Maven?

This descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

What is goal single in Maven?

This goal is suitable either for binding to the lifecycle or calling directly from the command line (provided all required files are available before the build starts, or are produced by another goal specified before this one on the command line).

What is Maven Assembly plug in?

The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files. Your project can easily build distribution "assemblies" using one of the prefabricated assembly descriptors.


2 Answers

If your resources have a pattern (say *.properties) then you can do something like this in your assembly descriptor file:

<fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.properties</include>
            </includes>
        </fileSet>
</fileSets>

This copies all *.properties from your target folder to the root folder of your assembly zip. Based on the profile in your pom.xml which is being run, only appropriate resources will be present in the target folder.

like image 117
Raghuram Avatar answered Nov 09 '22 22:11

Raghuram


Place the executions of the plugin in every profile, inside tag build.

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <phase>grr</phase>
            <goals>
              <goal>tree</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <excludes>
            <exclude>**/*.xml</exclude>
          </excludes>
        </configuration>
      </plugin>

    </plugins>
  </build>
  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.1</version>
            <configuration>
              <descriptors>
                <descriptor>descriptor.xml</descriptor>
              </descriptors>
              <finalName>xxx-impl-${pom.version}</finalName>
              <outputDirectory>target/assembly</outputDirectory>
              <workDirectory>target/assembly/work</workDirectory>
            </configuration>
          </plugin>
          <resources>
            <resource>
              <directory>src/install/dev</directory>
              <includes>
                <include>**/*</include>
              </includes>
            </resource>
          </resources>
      </build>
    </profile>
    <profile>
      <id>prod</id>
      <build>
        <resources>
          <resource>
            <directory>src/install/prod</directory>
            <includes>
              <include>**/*</include>
            </includes>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
like image 26
ssedano Avatar answered Nov 09 '22 22:11

ssedano