views:

102

answers:

2

I want to add to a jar file some resources. I "profiled" it and added them in the build section.

But the resources aren't in the final jar file.

Here it goes the profile section of my pom.xml:

<profile>
  <id>myProfile</id>
  <build>
    <finalName>name</finalName>
    <resources>
      <resource>
        <targetPath>.</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/profiles/myFolder</directory>
        <includes>
          <include>file.txt</include>
          <include>file.xml</include>
        </includes>
      </resource>
    </resources>
  </build>
</profile>

And here the command I issue:

mvn clean install -PmyProfile

What's wrong?

+1  A: 

Resources can be specified in two ways in maven. The simple one are just directories which are copied to target/classes directory. You can just configure resource directories, filtering and included/excluded files. Default resource directory is src/main/resources.

For complex setting you need to configure maven plugin. When you run install following actions run. In packaging phase runs maven-resources-plugin with resources:resources goal.

For the configuration use only resources mojo parameters. See example.

Move your profiles to ${projectdir}/profiles/myFolder.

<resources>
    <resource>
         <directory>profiles/myFolder</directory>
         <includes>
             <include>file.txt</include>
             <include>file.xml</include>
         </includes>
    </resource>
</resources>

You can omit includes part if profiles/myFolder contains only files to include.

amra
@amra: Thank you for your time, as I said to Pascal, I'll try upgrading maven and I will post results. Thank you.
Udo Fholl
+1  A: 

Your POM snippet looks globally fine and I cannot reproduce your problem. Here is the pom.xml I used:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q3459013</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <profiles>
    <profile>
      <id>myProfile</id>
      <build>
        <finalName>name</finalName>
        <resources>
          <resource>
            <directory>${basedir}/profiles/myFolder</directory>
            <filtering>false</filtering>
            <includes>
              <include>file.txt</include>
              <include>file.xml</include>
            </includes>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
</project>

With the following project structure:

$ tree .
.
├── pom.xml
├── profiles
│   └── myFolder
│       ├── file.txt
│       └── file.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── stackoverflow
    │               └── App.java
    └── test
        └── java
            └── com
                └── stackoverflow
                    └── AppTest.java

11 directories, 5 files

And here is what I get:

$ mvn clean install -PmyProfile
...
$ cd target
$ tree .
.
├── classes
│   ├── com
│   │   └── stackoverflow
│   │       └── App.class
│   ├── file.txt
│   └── file.xml
├── maven-archiver
│   └── pom.properties
├── name.jar
├── surefire-reports
│   ├── com.stackoverflow.AppTest.txt
│   └── TEST-com.stackoverflow.AppTest.xml
└── test-classes
    └── com
        └── stackoverflow
            └── AppTest.class
$ jar xvf name.jar 
  created: META-INF/
 inflated: META-INF/MANIFEST.MF
  created: com/
  created: com/stackoverflow/
 inflated: file.xml
 inflated: com/stackoverflow/App.class
 inflated: file.txt
  created: META-INF/maven/
  created: META-INF/maven/com.stackoverflow/
  created: META-INF/maven/com.stackoverflow/Q3459013/
 inflated: META-INF/maven/com.stackoverflow/Q3459013/pom.xml
 inflated: META-INF/maven/com.stackoverflow/Q3459013/pom.properties

Works as expected.

Pascal Thivent
@Pascal: very nice answer. I have same pom.xml as you it keeps not including in the jar file the proper files. I'll try later with a newer version of maven2 (mine is 2.0.9). Thank you for you time!
Udo Fholl
@Udo I'm indeed using Maven 2.2.1
Pascal Thivent
@Pascal and amra: Now I got it to work thank you! but if I want to place the files in root folder (jar)? targetPath>target/myProject</targetPath> doesnt work
Udo Fholl
@Udo I'm not sure I understood the question but during `package`, Maven jars the content of `target/classes`. If you put things outside `target/classes`, it won't end up in the jar. And instead of playing with `targetPath`, I suggest creating the appropriate directory structure under the `resource` directory.
Pascal Thivent
@Pascal: sorry for the delay and thanks. My problem is to place a file from one folder to the root directory of the jar. I've been messing with targetPath but I can't get itt o work.Thanks!
Udo Fholl
@Udo Placing a file to the root directory of the jar means getting it copied to `target/classes`. But I'm afraid I don't understand why don't succeed at doing so. Maybe if you update the question to show the specific tricky part I could help more.
Pascal Thivent