Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package name without main.java in maven

Tags:

I develop my project in Eclipse with standard Maven directory layout. Sources are in src/main/java/com/mycompany directory. And it is required to put my source classes in package main.java.com.mycompany. When I build executable jar-file, I have to define the main class in Manifest.MF like:

    Main-Class: main.java.com.mycompany.MyMainClass 

But I saw in a lot of examples (like configuring the mainClass attribute in maven-assembly-plugin or maven-jar-plugin) that class names are specified without main.java part. I just want to have

    Main-Class: com.mycompany.MyMainClass 

I can't figure out how I can achieve this because I'm completely novice in Maven.

like image 737
Grade Avatar asked Feb 09 '13 13:02

Grade


People also ask

What is package name in Maven?

While creating a maven project if you have mentioned values for both groupId and package name, then maven will consider the package name to place your java class. For example: mvn archetype:generate -DgroupId=gen.src -DartifactId=Iftekhar -DpackageName=com.src.Model -Dversion=2.0-Snapshot. In the above scenario App.

How do you name a package in Java?

Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .

Can we change package name Java?

Step 4: Now right-click on the first package name (com) and Refactor > Rename. A warning message will be displayed but go ahead and click on the Rename current button. Step 5: Rename the directory name as your requirement and click on the Refactor button. Note: Go to Build > Rebuild Project to display the updated name.


2 Answers

Maven's default directory layout differs from the layout Eclipse uses when you create a new Java project.

It's actually pretty simple. When you create a new Java project, Eclipse creates a folder named 'src' on your filesystem, where you should put your source code. Additionally, Eclipse configures this folder as a 'source folder'. Every folder you create in this folder will represent a Java package.

What Maven wants is a little bit different. Maven likes you to have a 'src/main/java' folder with the 'java' subfolder configured to be a source folder. Hence, all folders created under this java folder will represent Java packages.

You can manage your source folders in Eclipse from your project properties. Right click your project, select 'build path -> configure build path'. In the popup open the 'source' tab. Now, remove 'src' as a source folder and replace it with 'src/main/java'.

Now Eclipse will recognize your packages as com.mycompany instead of main.java.com.mycompany.

I always use the maven-eclipse plugin to generate my eclipse project and classpath files from the commmand line. I'm not a big fan of m2e.

like image 92
Rens Verhage Avatar answered Oct 02 '22 16:10

Rens Verhage


You can also try updating the maven project. Right click on the project > Maven > Update Maven Project.

like image 25
Ankur Kaul Avatar answered Oct 02 '22 16:10

Ankur Kaul