thee
thee

Reputation: 520

Include a file multiple times with different names in a Maven-built JAR

I work with Java resource bundles that are included in the final JAR using Maven's resource tag:

<resources>
    <resource>
        <targetPath>lang/</targetPath>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources/</directory>
        <includes>
            <include>localization*.properties</include>
        </includes>
    </resource>
</resources>

Due to the nature of the automatic resource loading, I need to include the English file two times: As the standard English file (localization_en.properties) and as the base file that provides a fallback if a more specific localization is not found (localization.properties). At the moment, both of these files are present in the resource directory, even through their content is exactly the same.

I am looking for a way that lets Maven duplicate the present localization_en.properties and include it with the base name, so I do not need two separated files in the resource directory any more.

Upvotes: 2

Views: 1411

Answers (2)

I believe that you can do what you need using the ant copy task. Something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
         <id>copy-files</id>
         <phase>compile</phase>
            <goals>
               <goal>run</goal>
            </goals>
            <configuration>
               <target name="copy your files">
                   <copy file="a.txt" tofile="a_eng.txt" />
                   <copy file="a.txt" tofile="a.txt" />
                </target>
             </configuration>                       
          </execution>
       </executions>
 </plugin>

Upvotes: 1

Duncan Jones
Duncan Jones

Reputation: 69339

You can use the Maven assembly plugin to achieve this. Essentially you build a JAR from scratch, using the classes in your target directory (which will already include your normal copied resources), then add a copy of your specific resource with a new name.

Note, to exactly match what you would normally get in a build, you have to manually copy the pom.properties and pom.xml into the resulting JAR. Perhaps a passing commenter will know a way to do this automatically?

<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>example</id>
  <formats>
    <format>jar</format>
  </formats>

  <includeBaseDirectory>false</includeBaseDirectory>

  <!-- Copy all compiled classes and normal copied resources -->
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
      <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>       

  <files>
    <!-- Specifically add renamed file -->
    <file>
      <source>${basedir}/src/main/resources/example.txt</source>
      <destName>example.txt2</destName>
      <outputDirectory>/</outputDirectory>
    </file>

    <!-- Copy files normally included in JAR -->
    <file>
      <source>${project.build.directory}/maven-archiver/pom.properties</source>
      <outputDirectory>META-INF/maven/${project.groupId}/${project.artifactId}</outputDirectory>
    </file>
    <file>
      <source>${basedir}/pom.xml</source>
      <outputDirectory>META-INF/maven/${project.groupId}/${project.artifactId}</outputDirectory>
    </file>
  </files>
</assembly>

Note: if you intend to create a jar-with-dependencies style output (i.e. with all dependencies auto-included), there is a much neater way to achieve the same goal:

<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>example</id>
  <formats>
    <format>jar</format>
  </formats>

  <includeBaseDirectory>false</includeBaseDirectory>

  <!-- Make executable JAR -->
  <dependencySets>
    <dependencySet>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
    </dependencySet>
  </dependencySets>

  <files>
    <!-- Specifically add renamed file -->
    <file>
      <source>${basedir}/src/main/resources/example.txt</source>
      <destName>example.txt2</destName>
      <outputDirectory>/</outputDirectory>
    </file>
  </files>
</assembly>

Upvotes: 0

Related Questions