在Maven构建期间将文件添加到jar [英] Adding file to jar during maven build

查看:69
本文介绍了在Maven构建期间将文件添加到jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在执行Maven构建时向所有jar添加许可证文件.每个类文件都有许可证,但我想将License.txt添加到每个jar中的每个META-INF文件夹中

I am trying to add a license file to all of my jars when executing a maven build. I have the license on each class file, but I am looking to add License.txt to each META-INF folder within each jar

我的项目有一个主pom,其中有六个模块,然后这些模块具有自己的模块,并最终到达一个生成/target/<.jar-file>的项目.构建和类级别的许可证都可以使用,我只是想将物理的License.txt添加到META-INF文件夹中.

My project has a master pom, which has half dozen modules, those modules then have modules of their own, and eventually get to a project that generates a /target/<.jar-file>. The build and the class level licenses are working, I am just looking to add a physical License.txt into the META-INF folder.

我的文件(相对于主POM)存储在/src/resources/src-license.txt中.我真的需要一种自动化的方法来确保如果/当许可证更改时,我必须更新50个文件,我可以只更新一个文件,然后将其复制到其他位置.

My file is stored (relative to the master POM) in /src/resources/src-license.txt. I really need the automated method to ensure that if/when the license changes, I dotn have to update 50 files, I can just update the one, which is then copied out to the other locations.

我尝试使用

<build>
  <sourceDirectory>src</sourceDirectory>
  <resources>
    <resource>
      <directory>src/resources</directory>
      <targetPath>/META-INF</targetPath>
      <includes>
        <include>src-license.txt</include>
      </includes>
    </resource>
  </resources>
....
</build>

但这似乎并不能解决问题.我也尝试了一些输出路径的替代方法,例如$ {project.build.outputDirectory}/META-INF或*/META_INF,也无济于事.有没有人对如何做到这一点有经验?谢谢

But that doesnt seem to do the trick. I have also tries some alternatives to the output path, such as ${project.build.outputDirectory}/META-INF, or */META_INF, also to no avail. Does anyone have some experience on how to accomplish this? Thanks

此外,我使用maven-license-plugin来确保每个类文件都粘贴了许可证信息,并且可以按预期运行.但是同样,在类文件中,我正在每个< *.jar>/META-INF/

Also, I use the maven-license-plugin to ensure that each class file has the license info pasted into it, and that functions as intended. But again, that inside the class files, I am looking for an external .txt file in each <*.jar>/META-INF/

推荐答案

使用父parentrelativePath中的资源至少很困难(如果您具有复杂的目录结构,该怎么办?).

Using resources from parent relativePath is at least difficult (what if you have a complex directory structure?).

一种简单干净的方法是创建一个包含src-license.txt文件的单独模块.然后使其成为模块的依赖项,并在目标/类内部将其解压缩(dependency:unpack-dependencies)@ generate-resources阶段.

A simple and clean way can be to create a separate module containing your src-license.txt file. Then make it a dependency of your modules and unzip it (dependency:unpack-dependencies) @ generate-resources phase, inside target/classes.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>unpack-license</id>
                <phase>generate-resources</phase>
                <goals><goal>unpack</goal></goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>com.acme</groupId>
                            <artifactId>com.acme.license</artifactId>
                            <version>${project.version}</version>
                        </artifactItem>
                    </artifactItems>
                    <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>

这篇关于在Maven构建期间将文件添加到jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆