Reputation: 285
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 Project2I 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>
Upvotes: 2
Views: 3189
Reputation: 14971
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>
Upvotes: 1
Reputation: 8031
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.
Upvotes: 0