5

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. How can I add messages.properties?

Thanks.

Edit.

I am using addAsResource now but it only works for files that are under test/resources folder. How can I make it work with files under main/src? Is there any maven configuration for that?

The goal is to not duplicate files. Right now I have one file under main/src and a duplicate under test\resources.

rubenlop88
  • 4,171
  • 2
  • 28
  • 32

3 Answers3

7

I finally added this configuration to my 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. Therefore ShrinkWrap will find it in the classpath.

Oliver
  • 3,815
  • 8
  • 35
  • 63
rubenlop88
  • 4,171
  • 2
  • 28
  • 32
  • 1
    The trick is to add resource as a File `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)` – Jarek Przygódzki Dec 07 '15 at 16:03
2

You can use the addAsResource method to add the file. The method is defined here: https://github.com/shrinkwrap/shrinkwrap/blob/master/api/src/main/java/org/jboss/shrinkwrap/api/container/ResourceContainer.java#L86

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • I am using `asAsResource` now but it only works for files that are under `test/resources` folder. Do you know how to make it work with files under `main\src`? Is there any maven configuration that can help me? – rubenlop88 Sep 05 '13 at 21:45
  • Yes, just use the full path. `.addAsResource("src/main/foobar.properties","foobar.properties")` – John Ament Sep 06 '13 at 18:32
  • I've tried this before but it didn't work. I had to add `src/main` as a test resource. – rubenlop88 Sep 18 '13 at 18:13
  • @rubenlop88 - the trick is to add resource as a File `addAsResource(new File("src/main/foobar.properties"), "foobar.properties"))` – Jarek Przygódzki Dec 07 '15 at 16:01
2

The trick in this case is to add resource as a File:

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).

Jarek Przygódzki
  • 4,284
  • 2
  • 31
  • 41