我应该在哪里放置Maven项目的应用程序配置文件?

时间:2023-01-13 23:06:42

I'm using the Maven Application Assembler plugin to generate stand-alone executables from my Java project. The application reads in configuration files, including Spring files. The Application Assembler plugin has an option (activated by default) to add a etc/ directory to the application's classpath, but what should I do to have the plugin copy my configuration files to this directory?

我正在使用Maven Application Assembler插件从我的Java项目生成独立的可执行文件。应用程序读入配置文件,包括Spring文件。 Application Assembler插件有一个选项(默认激活)将etc /目录添加到应用程序的类路径中,但是如何让插件将我的配置文件复制到此目录中呢?

Or more generally, where is in Maven the kosher location for application configuration files that should NOT be packaged in the artifact?

或者更一般地说,Maven中应用程序配置文件的犹太位置在哪里不应该打包在工件中?

5 个解决方案

#1


You can also use resource filtering: http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files

您还可以使用资源过滤:http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files

turn on filtering:

打开过滤:

...
<build>
...
<resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
...
</build>
...

make a file under src/main/resources like: application.properties

在src / main / resources下创建一个文件,例如:application.properties

application.properties

configprop.1=${param1}
configprop.2=${param2}

Then setup a profile and set some properties perhaps in a settings.xml that sets different properties depending on if this is a dev or production build. see: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

然后设置一个配置文件并在settings.xml中设置一些属性,这些属性设置不同的属性,具体取决于它是dev还是生成版本。请参阅:http://maven.apache.org/guides/introduction/introduction-to-profiles.html

I have different properties set depending on if this is the build server, dev or a production deployment

我有不同的属性设置,具体取决于它是构建服务器,开发还是生产部署

mvn -Denv=dev || mvn -Denv=dev-build || mvn -Denv=production

mvn -Denv = dev || mvn -Denv = dev-build || mvn -Denv =生产

The maven link has a pretty good description.

maven链接有很好的描述。

#2


For folks who have come to this more recently there is, since version 1.1 of the Application Assembler Plugin, the optional parameters configurationSourceDirectory and copyConfigurationDirectory. Please find them in an example POM.xml extract below:

对于最近出现这种情况的人来说,自应用程序汇编程序插件的1.1版以来,可选参数configurationSourceDirectory和copyConfigurationDirectory。请在下面的示例POM.xml提取中找到它们:

<configuration>
  <!-- Set the target configuration directory to be used in the bin scripts -->
  <configurationDirectory>conf</configurationDirectory>
  <!-- Copy the contents from "/src/main/config" to the target
      configuration directory in the assembled application -->
  <copyConfigurationDirectory>true</copyConfigurationDirectory>
  <!-- Include the target configuration directory in the beginning of
      the classpath declaration in the bin scripts -->
  <includeConfigurationDirectoryInClasspath>
    true
  </includeConfigurationDirectoryInClasspath>
...
</configuration>

More information is here

更多信息在这里

#3


You could try the maven assembly plugin. I used it in conjunction with the appassembler plugin.

你可以尝试maven组件插件。我将它与appassembler插件结合使用。

Configure appassembler to point to whatever name you want for your configuration directory, if you don't want 'etc'. The assembly plugin assembles everything in its own output directory, so I configure the assembly plugin to copy the bin and repo dirs from the appassembler directory into its output dir, then I have it copy the config files (mine are in src/main/config) into the expected config dir. There is some duplication in this, because you are copying the appassembler outputs, but that didn't really bother me.

如果您不想要'etc',请将appassembler配置为指向配置目录所需的任何名称。程序集插件在其自己的输出目录中汇编所有内容,因此我配置程序集插件将bin和repo dirs从appassembler目录复制到其输出目录中,然后我将其复制配置文件(我的是在src / main / config中)进入预期的配置目录。这有一些重复,因为你正在复制appassembler输出,但这并没有真正打扰我。

So what you have after executing the assembly plugin is your bin, repo, and config dir are all peer directories under the assembly output directory. You can configure it to have a different structure if you prefer, I just wanted mine to mirror the appassembler structure.

所以你在执行程序集插件后所拥有的是你的bin,repo和config dir都是程序集输出目录下的所有对等目录。如果您愿意,可以将其配置为具有不同的结构,我只是希望我的镜像appassembler结构。

The nice thing is that you can also configure the assembly plugin to change your binaries to executables, which I could't see how to do with appassembler. And, if you then bind appassembler:assemble and assembly:single goals to the package phase, all you have to do is 'mvn package', and it assembles everything.

好处是您还可以配置程序集插件以将二进制文件更改为可执行文件,我无法看到如何使用appassembler。而且,如果你然后绑定appassembler:汇编和汇编:单个目标到包阶段,你所要做的就是'mvn package',它汇集了所有东西。

#4


I don't know if I understand you correctly. But what I have done in the past for a project where I needed to copy configuration files, is use the Maven AntRun plugin. What I did is execute the plugin in the process-resources phase and copied my configuration files to the specified directory using the Ant copy task. The Assembler plugin executes in the package phase so it should pick up your configuration files if you put it in the right place. Hope this answers your question a little bit.

我不知道我是否理解正确。但是我过去为一个需要复制配置文件的项目所做的就是使用Maven AntRun插件。我所做的是在process-resources阶段执行插件,并使用Ant复制任务将我的配置文件复制到指定的目录。 Assembler插件在包阶段执行,因此如果您将配置文件放在正确的位置,它应该选择配置文件。希望这能回答你的问题。

#5


I had been looking for an answer to what I think is your question, or at least a very similar question. Maven allows you to specify directories for resources using the maven-resources-plugin. I have a few configuration files in one of my resource directories. I've noticed that by putting copies of those files in the etc/ directory that you mention (which is at the beginning of my CLASSPATH) I can change values in those files for use at run time. I then wanted to have that etc/ directory created with copies of everything from my resource directory by default. The copy-resources goal from the maven-resources-plugin allowed me to do that. This stanza from Examples > Copy Resources on the left sidebar (I'm limited to 2 links in this post) is what did it for me:

我一直在寻找我认为你的问题的答案,或者至少是一个非常相似的问题。 Maven允许您使用maven-resources-plugin为资源指定目录。我的一个资源目录中有一些配置文件。我注意到通过将这些文件的副本放在你提到的etc /目录中(这是我的CLASSPATH的开头),我可以更改这些文件中的值以便在运行时使用。然后,我希望默认情况下使用资源目录中的所有内容副本创建etc /目录。 maven-resources-plugin的copy-resources目标允许我这样做。左侧边栏上的示例>复制资源(我在这篇文章中仅限于2个链接)中的这个节对我来说是这样做的:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

#1


You can also use resource filtering: http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files

您还可以使用资源过滤:http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files

turn on filtering:

打开过滤:

...
<build>
...
<resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
...
</build>
...

make a file under src/main/resources like: application.properties

在src / main / resources下创建一个文件,例如:application.properties

application.properties

configprop.1=${param1}
configprop.2=${param2}

Then setup a profile and set some properties perhaps in a settings.xml that sets different properties depending on if this is a dev or production build. see: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

然后设置一个配置文件并在settings.xml中设置一些属性,这些属性设置不同的属性,具体取决于它是dev还是生成版本。请参阅:http://maven.apache.org/guides/introduction/introduction-to-profiles.html

I have different properties set depending on if this is the build server, dev or a production deployment

我有不同的属性设置,具体取决于它是构建服务器,开发还是生产部署

mvn -Denv=dev || mvn -Denv=dev-build || mvn -Denv=production

mvn -Denv = dev || mvn -Denv = dev-build || mvn -Denv =生产

The maven link has a pretty good description.

maven链接有很好的描述。

#2


For folks who have come to this more recently there is, since version 1.1 of the Application Assembler Plugin, the optional parameters configurationSourceDirectory and copyConfigurationDirectory. Please find them in an example POM.xml extract below:

对于最近出现这种情况的人来说,自应用程序汇编程序插件的1.1版以来,可选参数configurationSourceDirectory和copyConfigurationDirectory。请在下面的示例POM.xml提取中找到它们:

<configuration>
  <!-- Set the target configuration directory to be used in the bin scripts -->
  <configurationDirectory>conf</configurationDirectory>
  <!-- Copy the contents from "/src/main/config" to the target
      configuration directory in the assembled application -->
  <copyConfigurationDirectory>true</copyConfigurationDirectory>
  <!-- Include the target configuration directory in the beginning of
      the classpath declaration in the bin scripts -->
  <includeConfigurationDirectoryInClasspath>
    true
  </includeConfigurationDirectoryInClasspath>
...
</configuration>

More information is here

更多信息在这里

#3


You could try the maven assembly plugin. I used it in conjunction with the appassembler plugin.

你可以尝试maven组件插件。我将它与appassembler插件结合使用。

Configure appassembler to point to whatever name you want for your configuration directory, if you don't want 'etc'. The assembly plugin assembles everything in its own output directory, so I configure the assembly plugin to copy the bin and repo dirs from the appassembler directory into its output dir, then I have it copy the config files (mine are in src/main/config) into the expected config dir. There is some duplication in this, because you are copying the appassembler outputs, but that didn't really bother me.

如果您不想要'etc',请将appassembler配置为指向配置目录所需的任何名称。程序集插件在其自己的输出目录中汇编所有内容,因此我配置程序集插件将bin和repo dirs从appassembler目录复制到其输出目录中,然后我将其复制配置文件(我的是在src / main / config中)进入预期的配置目录。这有一些重复,因为你正在复制appassembler输出,但这并没有真正打扰我。

So what you have after executing the assembly plugin is your bin, repo, and config dir are all peer directories under the assembly output directory. You can configure it to have a different structure if you prefer, I just wanted mine to mirror the appassembler structure.

所以你在执行程序集插件后所拥有的是你的bin,repo和config dir都是程序集输出目录下的所有对等目录。如果您愿意,可以将其配置为具有不同的结构,我只是希望我的镜像appassembler结构。

The nice thing is that you can also configure the assembly plugin to change your binaries to executables, which I could't see how to do with appassembler. And, if you then bind appassembler:assemble and assembly:single goals to the package phase, all you have to do is 'mvn package', and it assembles everything.

好处是您还可以配置程序集插件以将二进制文件更改为可执行文件,我无法看到如何使用appassembler。而且,如果你然后绑定appassembler:汇编和汇编:单个目标到包阶段,你所要做的就是'mvn package',它汇集了所有东西。

#4


I don't know if I understand you correctly. But what I have done in the past for a project where I needed to copy configuration files, is use the Maven AntRun plugin. What I did is execute the plugin in the process-resources phase and copied my configuration files to the specified directory using the Ant copy task. The Assembler plugin executes in the package phase so it should pick up your configuration files if you put it in the right place. Hope this answers your question a little bit.

我不知道我是否理解正确。但是我过去为一个需要复制配置文件的项目所做的就是使用Maven AntRun插件。我所做的是在process-resources阶段执行插件,并使用Ant复制任务将我的配置文件复制到指定的目录。 Assembler插件在包阶段执行,因此如果您将配置文件放在正确的位置,它应该选择配置文件。希望这能回答你的问题。

#5


I had been looking for an answer to what I think is your question, or at least a very similar question. Maven allows you to specify directories for resources using the maven-resources-plugin. I have a few configuration files in one of my resource directories. I've noticed that by putting copies of those files in the etc/ directory that you mention (which is at the beginning of my CLASSPATH) I can change values in those files for use at run time. I then wanted to have that etc/ directory created with copies of everything from my resource directory by default. The copy-resources goal from the maven-resources-plugin allowed me to do that. This stanza from Examples > Copy Resources on the left sidebar (I'm limited to 2 links in this post) is what did it for me:

我一直在寻找我认为你的问题的答案,或者至少是一个非常相似的问题。 Maven允许您使用maven-resources-plugin为资源指定目录。我的一个资源目录中有一些配置文件。我注意到通过将这些文件的副本放在你提到的etc /目录中(这是我的CLASSPATH的开头),我可以更改这些文件中的值以便在运行时使用。然后,我希望默认情况下使用资源目录中的所有内容副本创建etc /目录。 maven-resources-plugin的copy-resources目标允许我这样做。左侧边栏上的示例>复制资源(我在这篇文章中仅限于2个链接)中的这个节对我来说是这样做的:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>