0

I'm struggling with this issue for a while now, i have a maven project in IntelliJ with 2 maven modules.

When i'm in the java folder (sources root) of my 2nd module, when i create a class, i can reference to the classes of my other project. But when i'm inside a package in the java folder, i am no longer able to use there classes of the other project.

Thanks in advance,

Link to images: https://i.stack.imgur.com/Ya6SM.jpg

My POM.xml from the parent project:

<?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>
<packaging>pom</packaging>

<groupId>com.ucll.da</groupId>
<artifactId>project2</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
    <module>domain</module>
    <module>webApp</module>
    <module>rest</module>
</modules>

</project>

The POM files from domain and rest are basically the same:

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

<parent>
    <artifactId>project2</artifactId>
    <groupId>com.ucll.da</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>domain</artifactId>

</project>
denelias
  • 21
  • 1
  • 7
  • The picture shows two classes in different packages within a module. Do you mean packages instead of modules by chance? – swiedenfeld Dec 27 '16 at 16:36
  • No I have an other maven module (called 'domain') which has the class ObservationService. In my 'rest' module, I can only use my ObservationService in a class which is in the java folder and not in a package under the java folder. – denelias Dec 27 '16 at 16:42
  • I recommend you update your question with relevant snippets of the dependencies from pom.xml and the package structure. – swiedenfeld Dec 27 '16 at 16:44
  • Please paste the code in the question instead of linking an image first along with the error logs and relevant details from pom.xml. – Naman Dec 27 '16 at 16:59

1 Answers1

0

I found my solution but i don't really know why it works now.

My java files in the first Maven module (domain) were not inside a package, just inside the map Java. When I created a new package inside this Java map, and moved all my java files there, I could use these classes inside the other Maven module.

denelias
  • 21
  • 1
  • 7
  • When you place classes as children of `/src/main/java`, they become part of the unnamed package. Use of unnamed package is discouraged, see this question http://stackoverflow.com/q/7849421/6850448. You cannot import classes in unnamed package from named packages. – swiedenfeld Dec 28 '16 at 19:30
  • Thank you for this explanation :) – denelias Dec 29 '16 at 21:19