How to generate a resource to be included in Jar file with maven?

yankee

During my build I need to some files to be generated by an external tool. For my minimal compilable example I reduced my "external tool" to the following script:

mkdir -p target/generated-resources
echo "hello world" > target/generated-resources/myResource.txt

Now I want to execute my external tool during build and the generated resource should be included in the war file. I could not find any documention on how that should be done, so it was just a guess that I need to write my generated resource to target/generated-resources. So maybe that is a problem?

I created a pom.xml file with the following build configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>./createResource.sh</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

If I run mvn package my createResource.sh script gets executed successfully and the myResource.txt file is created. However the myResource.txt file is not included in the resulting .jar file.

I noticed that it works if I add

<resources>
  <resource>
    <directory>target/generated-resources</directory>
  </resource>
</resources>

to my build config, but I fear that this may cause problems if other plugins which may use this directory differently (Do they? I could not really find anything about the conventions of the target directory).

Additionally I'd prefer a solution that works with the usual maven conventions (if a convention for this case exists).

How do I correctly generate a resource to be included in the jar file during build using maven?

Tome

Convention (or main usage at least)for Maven is to generate resources inside (target/generated-resources/[plugin/process]). But unlike generated sources and compiler plugin, generated resources are not handled specifically by the jar plugin, so you do have to add it as a resource (with a new resource like you did or the build-helper-plugin).

If you follow the convention to place everything you generate under a sub-directory of generated-resources, you should have no fear about how other plugins use it.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to see php error in included file while output buffer?

来自分类Dev

Check if file is included on page

来自分类Dev

How to add classpath properties to executable jar using maven assembly

来自分类Dev

how to load jar:file: uri in swt browser?

来自分类Dev

Maven检索无效的jar

来自分类Dev

从Maven创建jar文件

来自分类Dev

覆盖JAR Maven

来自分类Dev

Maven检索无效的jar

来自分类Dev

Loop index in included nunjucks file

来自分类Dev

How to edit a resource file from within an android app? (Android Studio)

来自分类Dev

Maven 无法解析 Kotlin Maven 插件 jar

来自分类Dev

maven:从JDK中排除jar

来自分类Dev

Maven回购中缺少Jar

来自分类Dev

Maven jar签名者插件

来自分类Dev

用Maven执行jar命令

来自分类Dev

用依赖Maven构建jar

来自分类Dev

Maven 安装 jar 创建失败

来自分类Dev

maven 外部库 - jar 文件

来自分类Dev

German email resource file

来自分类Dev

Empty .jar File in Ecilipse

来自分类Dev

如何包括在Maven中加载其他jar的jar?

来自分类Dev

如何从maven-jar-plugin的jar中排除Java文件?

来自分类Dev

在MAVEN中使用外部JAR制作JAR的问题

来自分类Dev

无法使用 maven-jar-plugin 构建 jar

来自分类Dev

Using a Resource File on buttons OnClientClick

来自分类Dev

Maven的依赖插件:排除.jar文件

来自分类Dev

Maven Jar中的静态Web资源?

来自分类Dev

直接从Maven存储库执行JAR文件

来自分类Dev

如何使用Maven组织Hadoop的生成jar