简体   繁体   English

如何将'main / src / ... / messages.properties'文件添加到ShrinkWrap WebArchive?

[英]How to add 'main/src/…/messages.properties' file to ShrinkWrap WebArchive?

I have the following files in a project: 我在项目中有以下文件:

com/example/module/Messages.java
com/example/module/messages.properties

Using ShrinkWrap.create(WebArchive.class, "test.war").addPackages(true, "com.example.module") only adds Messages.java to the generated archive. 使用ShrinkWrap.create(WebArchive.class, "test.war").addPackages(true, "com.example.module")仅将Messages.java添加到生成的存档中。 How can I add messages.properties ? 如何添加messages.properties

Thanks. 谢谢。

Edit. 编辑。

I am using addAsResource now but it only works for files that are under test/resources folder. 我现在正在使用addAsResource但它仅适用于test/resources文件夹下的文件。 How can I make it work with files under main/src ? 如何使它与main/src下的文件一起使用? Is there any maven configuration for that? 那有没有maven配置?

The goal is to not duplicate files. 目标是不重复文件。 Right now I have one file under main/src and a duplicate under test\\resources . 现在我在main/src下有一个文件,在test\\resources下有一个副本。

I finally added this configuration to my POM: 我终于将这个配置添加到了我的POM:

<build>
  <testResources>
    <testResource>
      <directory>${basedir}/src/main/java/</directory>
      <includes>
        <include>**/*.properties</include>
      </includes>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </testResource>
    <testResource>
      <directory>${basedir}/src/test/resources/</directory>
    </testResource>
  </testResources>
</build>

Then I added the properties file with: 然后我添加了属性文件:

.addAsResource("com/example/module/messages.properties")

Now Maven copies my messages.properties to the directory target/test-classes . 现在,Maven将我的messages.properties复制到目录target/test-classes Therefore ShrinkWrap will find it in the classpath. 因此ShrinkWrap会在类路径中找到它。

The trick in this case is to add resource as a File: 在这种情况下的技巧是将资源添加为文件:

addAsResource(new File("src/main/foobar.properties"), "foobar.properties")) . addAsResource(new File("src/main/foobar.properties"), "foobar.properties"))

Otherwise it must exist in the classpath - see org.jboss.shrinkwrap.impl.base.container.ContainerBase.fileFromResource(String resourceName) . 否则它必须存在于类路径中 - 请参阅org.jboss.shrinkwrap.impl.base.container.ContainerBase.fileFromResource(String resourceName)

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

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