2

I am currently switching from ant to maven for a larger library consisting of a couple of projects. I am pretty new to Maven.

Due to the fact, that multiple libraries require the same external files as input for testing, I decided to create an individual project containing the resources (Project1), as explained here. Project1 only contains the test resources, nothing else, except an class project1.java with an empty main method. The resource files are ASCII text and binary files with arbitrary file extensions. Now, I want to use these files in testing of other projects, e.g. Project2. I tried to illustrate my structure:

Root  
|-Project1: Resources
| |-src
| | |-main
|   |-test
|     |-resources
|       |-file1.txt
|       |-file2.dat
|       |-...
|-Project2: Consumer
| |-src
| | |-main
|   |-test
|     |...
|       |-test1.java (requires file1.txt)
|       |-test2.java (requires file2.dat)
|       |-...

I added Project1 as a dependency in the POM of Project2. Both projects are built with Clean and Build from Netbeans 11, so I guess mvn clean, build & install.

I do test for the existance of the relevant file in Project1 with Project1.class.getClassLoader().getResource([FILE]) and all files seem to be at the correct location. However, if I use Project1.class.getClassLoader().getResource([FILE]) from within Project2, e.g. test1.java the respective FILE is not found.

System.out.println("1:" + Project1.class.getClassLoader().getResource(""));
System.out.println("2:" + Project1.class.getClassLoader().getResource("file1.txt"));

from within test1.java in Project2 gives:

1: file:[PATHTOPROJECT]/Project2/target/test-classes/
2: null

So I tried following the apporach in this description. My problem with the explanation is, that I do not have a plugin to perform the executions for maven-shared-archive-resources as described in the consumer-part. Thus, I do not know where to put them. In the example the executions are carried out inside <groupId>org.codehaus.mojo</groupId> with <artifactId>sql-maven-plugin</artifactId>.

I also had a look at this thread, but I get an Ant BuildException.

Could somebody be so kind and explain a maven novice, how I can share resources between different projects? Is there even another, simpler way?


POM.xml of Project2

I tried to strip the POM.xml down to a MWE.

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!-- TOOL INFO -->
    <groupId>org.test</groupId>
    <artifactId>project2</artifactId>
    <version>0.0.0.1</version>
    <packaging>jar</packaging>

    <!-- PROPERTIES -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- System -->
        <java.version>1.8</java.version>
        <junit.jupiter.version>5.3.1</junit.jupiter.version>
        <junit.platform.version>1.0.0</junit.platform.version>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <maven.plugin.compiler.version>3.8.1</maven.plugin.compiler.version>
        <maven.plugin.enforcer.version>3.0.0-M2</maven.plugin.enforcer.version>
        <maven.plugin.release.version>2.5.3</maven.plugin.release.version>
        <maven.plugin.resources.remote.version>1.6.0</maven.plugin.resources.remote.version>
        <maven.plugin.surefire.version>3.0.0-M3</maven.plugin.surefire.version>
        <!-- Own -->
        <project1.version>0.0.0.1</project1.version>
    </properties>

    <!-- DEPENDENCIES -->
    <dependencies>
        <dependency>
            <groupId>org.test</groupId>
            <artifactId>project1</artifactId>
            <version>${project1.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- BUILD -->
    <build>

        <!-- PLUGINS -->
        <plugins>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.plugin.compiler.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>${maven.plugin.enforcer.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <rules>
                        <requireJavaVersion>
                            <version>${java.version}</version>
                        </requireJavaVersion>
                    </rules>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>${maven.plugin.release.version}</version>
                <configuration>
                    <localCheckout>true</localCheckout>
                    <pushChanges>false</pushChanges>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.plugin.surefire.version}</version>
            </plugin>
        </plugins>

    </build>
</project>
raedma
  • 293
  • 1
  • 12

2 Answers2

1

Change the location of the resources in project 1. Note, moved from test dir to main.

|-Project1: Resources
| |-src
| | |-main
|     |-resources
|       |-file1.txt
|       |-file2.dat
|       |-...

Then, in project 2's POM, add project 1 as a test dependency:

    <dependency>
        <groupId>org.test</groupId>
        <artifactId>project1</artifactId>
        <version>${project1.version}</version>
        <scope>test</scope>
    </dependency>
user944849
  • 14,524
  • 2
  • 61
  • 83
  • Will not work cause the resources are part of src/main/resources which are not packaged into test jar which is apart from that not enough. – khmarbaise May 21 '19 at 17:29
  • The resources get packaged into project1.jar (whatever the artifact name is). Then, we add project1.jar to project2's dependencies, as a test dependency. We've used this approach for other things and it works well. – user944849 May 21 '19 at 18:07
  • @user944849 thanks, now I at least see the files in project2. However, I get a `path syntax wrong` error when I want to create a ew `File` object with `File file = new File(Project1.class.getClassLoader().getResource("file1.txt").getFile());`. I'll post a follow-up question after some googling – raedma May 22 '19 at 08:26
  • 1
    The latter can be fixed by using the approach from https://stackoverflow.com/a/35466006/3154817 – raedma May 22 '19 at 08:49
0

Instead of sharing resources, create another project called "onlyresources" or something like this and inside the project keep only those *.txt, *.dat files. Other projects will have the dependency on the above defined project called "onlyresources". In case of multi module maven project, ensure to add the dependency to other projects. This project I mean "onlyresources" should be the first project which should be built first.

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • Hi, thanks for the tip. `Project1` only contains the test resources. Nothing else. And I did add it as a dependency to the `Project2` POM. However, Shouldn't Maven handle the correct build sequence then? – raedma May 21 '19 at 14:50
  • If you provide the pom.xml, we will be able to help you. You have to define the module name in module section. – Sambit May 21 '19 at 14:52
  • I added a stripped version of the pom, hopefully with the relevant parts. – raedma May 21 '19 at 14:59
  • First build the project Project1 with mvn clean install and then try run the second project with the same command. However you can create a multi module maven project. Check this link. http://websystique.com/maven/creating-maven-multi-module-project-with-eclipse/ – Sambit May 21 '19 at 15:04
  • I added information about my build process to the question – raedma May 21 '19 at 15:11
  • I see your pom.xml, try to use multi module maven project. First create a parent pom.xml, inside define the module for each project. Make sure that each project has a pom.xml. It will solve the problem. – Sambit May 21 '19 at 15:23
  • Shouldn't my approach also work without a multi module maven project? I've never worked with one. The thing is, the existing projects are loosely coupled. They are all libraries I do use in other projects, but not all at once mostly. So will I be able to make a dependency on a individual module or do I always have to specify the parent project in a multi module maven project as a dependency for tools using the library projects? – raedma May 21 '19 at 15:30
  • This approach may not work as you have completely independent project and you are trying to add the dependency. I will help you in this regard. Can you add the project1.jar file locally in maven and see whether it works. Find this link about how to use. https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project – Sambit May 21 '19 at 15:37