Question

Currently I have a sample maven project with the following build part where I specify a second resource location directory:

<project>
...
<build>
    <resources>
        <resource>
            <directory>src/main/second-resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <finalName>${artifactId}</finalName>
                <outputDirectory>${jarBuildPath}</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

I can get my jar packaged with this configuration having my classes and resource files just in right place and with no issue.

But when adding the property <classesDirectory> to my maven-jar-plugin configuration snippet with value src/main/java:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.4</version>
    <configuration>
      <classesDirectory>src/main/java</classesDirectory>
      <finalName>${artifactId}</finalName>
      <outputDirectory>${jarBuildPath}</outputDirectory>
    </configuration>
 </plugin>

then when packaging my archive, hell, my resource files are no longer there.

From maven official documentation for the classesDirectory property, it says that Directory containing the classes and resource files that should be packaged into the JAR. and that makes all sense for me and it is quite fair that my resource files get disappeared since maven assumes that no file has the right to be packaged unless it is under src/main/java.

But my big thought was when specifying my resource files location (with one of the options below), Maven will be aware of the files location even if I had specified the <classesDirectory> entry.

With top level <resources> entry

<resources>
  <resource>
  <directory>src/main/second-resources</directory>
  </resource>
</resources>

or via the maven-resource-plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.6</version>
    <configuration>
      <resources>
        <resource>
          <directory>src/main/second-resources</directory>
        </resource>
      </resources>
    </configuration>
  </plugin>

I can tell you that I'm quite familiar with maven but this issue seemed weird for me: Is it not recommended to have in maven-jar-plugin when you have a some custom resource directory?

Can anyone kindly drop some light on this?

BR.

Was it helpful?

Solution

The classesDirectory is the directory where

  • the compiler plugin stores compiled java classes
  • the resources-plugin copies resources to be included in the file created by the jar-plugin.

You have changed it to your java source code directory in the jar-plugin configuration.

Therefore, I imagine that your jar now only contains java source files.

Is this what you want, or are you just trying to include a second source of resource files?

If the latter, then just adding:

<resources>
  <resource>
      <directory>src/main/resources</directory>
  </resource>
  <resource>
      <directory>src/main/second-resources</directory>
  </resource>
</resources>

should accomplish what you want without adding any configuration to the jar plugin. Note that the first <resource>..</resource> section is needed if you still have stuff you want in the default src/main/resources location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top