2014-06-09 69 views
1

我与被包含在最终的JAR Java资源包的工作不同的名字多次使用Maven的资源标签:由于自动资源的性质包含的文件在一个Maven构建的JAR

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

加载时,我需要包含英文文件两次:作为标准英文文件(localization_en.properties)和作为基础文件,如果找不到更具体的本地化(localization.properties),则提供后备文件。目前,这两个文件都存在于资源目录中,即使它们的内容完全相同。

我正在寻找一种让Maven复制当前localization_en.properties并将其与基本名称一起包含的方式,因此我不再需要资源目录中的两个分隔文件。

+0

我不认为我们可以使用maven复制或附加到文件。你应该创建这个自定义appender,然后将该文件插入到目标文件中,之后maven可以使用它来构建 – vikeng21

回答

1

我相信你可以用蚂蚁拷贝任务来做你需要的。例如:

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

您可以使用Maven assembly插件来实现此目的。从本质上讲,您可以使用目标目录中的类(它将包含您的正常复制资源)从头开始构建JAR,然后使用新名称添加特定资源的副本。

注意,以究竟比赛你通常会在构建得到的,你有pom.propertiespom.xml手动复制到生成JAR。也许一个合格的评论者会知道自动执行此操作的方法?

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

注意:如果你打算创建一个jar-具有依赖性风格输出(即所有的依赖关系自动含税),有以达到同样的目标了一个非常巧妙的方法:

<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> 
相关问题