3

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.

paulpdaniels
  • 18,395
  • 2
  • 51
  • 55
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • 1
    What are you trying to accomplish? src/main/resources is included _by default_. – bmargulies May 07 '14 at 10:52
  • @bmargulies thanks for attention. Yes I'm aware of that two but actually I was thinking of that when having another cutom resource files directory/path. – tmarwen May 07 '14 at 11:01
  • You put that in the top-level element, not in the plugin config. – bmargulies May 07 '14 at 11:01
  • I will edit my quetion to be more significant. – tmarwen May 07 '14 at 11:02
  • 2
    The tag `` should contain `.class` files and therefore it should point to `target/classes` (which is the default) instead of `src/main/java`. If you want to bundle source files, take a look at the [source plugin](http://maven.apache.org/plugins/maven-source-plugin/) – Absurd-Mind May 07 '14 at 11:15
  • @Absurd-Mind thanks for the answer. So the `` should be used to point to compiled classes (.class) directory and not as I thought to sources one (.java)? – tmarwen May 07 '14 at 11:23
  • 1
    Yes, `class` is always a hint for compiled files (i.e. .class files), `source` on the other hand are `.java` files – Absurd-Mind May 07 '14 at 11:37

1 Answers1

3

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.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • So if I want to put a diffent `` in the *maven-jar-plugin* I should also alter both resource and compile plugin target paths (where they store `.class` and resource files*), right? How could I do that? – tmarwen May 07 '14 at 11:43
  • Are you deliberately trying to include java source files in your jar? Perhaps you should explain exactly what you're trying to do. – Steve C May 07 '14 at 11:47
  • The main issue is now fairly clear since all was about confusing classes and sources. But I just wanted to know how could I do to make maven-jar-plugin read classes from another directory rather than _target/classes_? How shoul maven-resource/compile-plugins be respectivelly configured? Any idea? – tmarwen May 07 '14 at 12:04