maven resources not placed in jar file

39,938

Solution 1

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

Solution 2

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.

Solution 3

Try this kind of simple structure, this is working for me.

<profile>
      <id>myProfile</id>
      <build>
        <finalName>name</finalName>
        <resources>
          <resource>
                <targetPath>webapp</targetPath>
                <directory>src/main/webapp</directory>
          </resource>
          <resource>
                <directory>src/main/resources</directory>
          </resource>
        </resources>
      </build>
    </profile>

Solution 4

Problem is: <targetPath>.</targetPath>, kick it off to fix problem.

To pack jar maven uses content of target/classes folder, otherwise use maven-jar-plugin and configure self what to copy.

Share:
39,938
ssedano
Author by

ssedano

Software developer!

Updated on December 08, 2020

Comments

  • ssedano
    ssedano over 3 years

    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?