简体   繁体   English

为Runnable Jars使用不同的属性文件,Maven Eclipse

[英]Using Different Properties Files for Runnable Jars, Maven Eclipse

I am new to Maven. 我是Maven的新手。 I apologize if this is a silly question. 如果这是一个愚蠢的问题,我道歉。 I have a Maven Eclipse project with a pom file that creates a runnable jar using the shade plugin. 我有一个带有pom文件的Maven Eclipse项目,该文件使用shade插件创建一个可运行的jar。 My plan is to include one of several properties files in the build (please correct me if my terminology is incorrect. I really am green). 我的计划是包括在建几个属性文件一个 (请纠正我,如果我的术语不正确。我真的是绿色的)。 I have multiple properties files located in src/main/resources. 我有多个属性文件位于src / main / resources中。

I would like to find a way to include in the runnable jar only the properties file that applies. 我想找到一种方法在runnable jar中只包含适用的属性文件。 Specifically, I have two properties files (config1.properties, config2.properties) in my project and each build will only use one of these files depending on the desired functionality of the resulting jar. 具体来说,我的项目中有两个属性文件(config1.properties,config2.properties),每个构建只使用其中一个文件,具体取决于生成的jar的所需功能。 The properties file would be reassigned a generic name, specifically "defaultconfig.properties", so that it plays nicely with the code. 属性文件将被重新分配一个通用名称,特别是“defaultconfig.properties”,以便它可以很好地与代码一起使用。

The resulting jar files (a different one for each different properties file) will run as separate cron jobs. 生成的jar文件(每个不同属性文件的不同文件)将作为单独的cron作业运行。 I think I will be using different Jenkins projects to deploy the runnable jars to specific servers based on what task the properties file configures the project for (I think I got this part). 我想我将使用不同的Jenkins项目根据属性文件配置项目的任务将可运行的jar部署到特定的服务器(我想我已经得到了这个部分)。 The cron jobs will run on these servers. cron作业将在这些服务器上运行。

my questions are: 我的问题是:

1) where does the logic go that determines which properties file to include in the runnable jar? 1)逻辑在哪里确定哪些属性文件包含在可运行的jar中?

for example, suppose I have config1.properties (that specifies to use var1 = blah and var2 = blip and var3 = boink) and config2.properties (which specifies to use var1 = duh var2 = dip and var3 = doink). 例如,假设我有config1.properties(指定使用var1 = blah和var2 = blip和var3 = boink)和config2.properties(指定使用var1 = duh var2 = dip和var3 = doink)。 Where do I tell Maven which of these two files to use for specific builds? 我在哪里告诉Maven这两个文件中的哪一个用于特定版本?

2) am I supposed to pass in a parameter that tells Maven which properties file to use? 2)我应该传递一个参数,告诉Maven使用哪个属性文件? If so, who do I pass this parameter too? 如果是这样,我也会传递这个参数? Is this something that can be configured in a Jenkins project? 这是可以在Jenkins项目中配置的吗? (even just some basic reading would help me here, as I am not sure if this would be a maven issue or a jenkins issue. I am green with both.) (即使只是一些基本的阅读会在这里帮助我,因为我不确定这是一个maven问题还是jenkins问题。我两个都是绿色的。)

Here is a the relevant portion of my pom.xml file: 这是我的pom.xml文件的相关部分:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.foo.dashboard.bar.Runner</mainClass>
                            </transformer>
                        </transformers>
                        <finalName>FooBar</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I have tried using IncludeResourceTransformer to rename the properties file and but was not sure how include the logic for switching properties files based on desired jar file functionality. 我已尝试使用IncludeResourceTransformer重命名属性文件,但不确定如何根据所需的jar文件功能包含切换属性文件的逻辑。

Thank you. 谢谢。

This is what I came up with. 这就是我提出的。 I would love to know other approaches or the best way to do this however. 我很想知道其他方法或最好的方法来做到这一点。 Being totally new to all of this. 对这一切都是全新的。

  1. Create separate executions in the same plugin. 在同一个插件中创建单独的执行。 Note: They must be assigned different <id> values. 注意:必须为它们分配不同的<id>值。
  2. Each <execution> can be configured differently. 每个<execution>可以配置不同。 To rename the resources that you are including use the <transfomer> implementation = implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">. 要重命名您包含的资源,请使用<transfomer> implementation = implementation =“org.apache.maven.plugins.shade.resource.IncludeResourceTransformer”>。
  3. To exclude resources that are not needed (in my case, other properties files) use <transformer> implementation = "org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">. 要排除不需要的资源(在我的情况下,其他属性文件),请使用<transformer> implementation =“org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer”>。 This is more nicety than necessity. 这比必要性更精确。 Why bundle up unneeded files in your jar? 为什么要在你的jar中捆绑不需要的文件?
  4. Make sure each <execution> has a different <finalName> so that the resulting jars don't overwrite each other. 确保每个<execution>具有不同的<finalName>以便生成的jar不会相互覆盖。

Here is my pom file. 这是我的pom文件。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>ID1</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.foo.dashboard.bar.Runner</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>defaultconfig.properties</resource>
                                <file>src/main/resources/defaultconfig_1.properties</file>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>defaultconfig_2.properties</resource>
                            </transformer>
                        </transformers>
                        <finalName>FooBar1</finalName>
                    </configuration>
                </execution>
                <execution>
                    <id>ID2</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.foo.dashboard.bar.Runner</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>defaultconfig.properties</resource>
                                <file>src/main/resources/defaultconfig_2.properties</file>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>defaultconfig_1.properties</resource>
                            </transformer>
                        </transformers>
                        <finalName>FooBar2</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I know this is not the final answer since this set up yields multiple jar files and I was originally looking for a solution that would give me only the jar file associated with a specific properties file, not all of the jar files from all of the properties files. 我知道这不是最终的答案,因为这个设置产生了多个jar文件,我最初寻找的解决方案只能给我一个与特定属性文件关联的jar文件,而不是所有属性的所有jar文件文件。

So I'm still taking suggestions on how to improve this solution so that the build would only a yield a single jar. 所以我仍在提出如何改进这个解决方案的建议,以便构建只产生一个jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM