Sam
Sam

Reputation: 1586

Using packages with Maven

For all Java projects I have participated in so far I have written build scripts using bash javac and java. However for my latest project I thought I would dive into Maven.

Whenever I compile my project I am getting compilation errors because a package doesn't exist. I have no external dependencies, but I do have sub packages within my application.

Example structure:

src/
    main/
        com/
            mycompany/
                /application
                    /package1
                        MyClass.java
                    /package2
                        MyClass2.java
                    App.java
    test/
pom.xml

Example Classes:
MyClass.java

package com.mycompany.application.package1;
public class MyClass{

    private MyClass(){}

}

App.java

package com.mycompany.application;
import com.mycompany.application.package1.MyClass;
public class App{

    public static void main(String[] args){
        System.out.println("It works!");
    }
}

Upvotes: 0

Views: 132

Answers (3)

asg
asg

Reputation: 2456

If you have imported your project in eclipse, please check your project's .classpath file have the following entry.

<classpathentry kind="src" output="target/classes" path="src/main/java"> 
  <attributes>
    <attribute name="optional" value="true"/> 
    <attribute name="maven.pomderived" value="true"/>
  </attributes>
</classpathentry>

Sometimes you need check .classpath file. This extra check has worked for me.

And are you executing maven commands from command line? If you executing - 'mvn clean install' like command from command prompt, ideally you should not get an error for the above mentioned directory structure without any changes.

Upvotes: 0

avaz
avaz

Reputation: 533

You should move your packages and classes to src/main/java, that is the root folder of packages in maven.

Hope it helps!

Upvotes: 2

SMA
SMA

Reputation: 37023

With Maven, you need to standardize your project structure. You just have src/main/, you should have it like src/main/java/

Maven by default will look all the java source/package within src/main/java folder.

See here for details about project structure.

Upvotes: 3

Related Questions