Maven suddenly wants src/ instead of src/main/java - eclipse

I'm using Maven 3.3.3 together with Eclipse Mars and m2e. Yesterday, I created a new Java project and converted it into a Maven one.
Afterwards, the classpath contained only src instead of the standard src/main/java. I was surprised but I simply created the missing folders and ran m2e's "Update Project". This didn't solve the problem; m2e still insisted on using only src.
So I edited the classpath manually to end up with with the usual
src/test/java
src/test/resources
src/main/java
src/main/resources
When I try m2e's "Update Project" now, I get this error:
Cannot nest 'foo/src/test/java' inside 'foo/src'. To enable the nesting, exclude 'test/' from 'foo/src'
sigh I went to the command line next and ran mvn eclipse:eclipse to get this classpath:
src/test/java
src/test/resources
src
src/main/resources
Now, I'm completely stumped. Why is that happening?

Open your POM in the POM editor and click on the "Effective POM" tab. Search for sourceDirectory. You'll probably see something like this:
<sourceDirectory>src</sourceDirectory>
When you first converted the Java project to Maven, m2e tried to keep the classpath the same. Eclipse Java projects have a different layout by default. They use src/ instead of src/main/java/. There is no test folder since Eclipse projects usually put their tests into a different project.
To fix the issue:
Delete the sourceDirectory element from the POM (Note: It might be in a parent POM).
Go to the project
Select all the source folders
Remove them from the build path (context menu -> Build Path -> Remove From Build Path).
Update the project
The error should now be gone and the classpath should be correct.

There are two types of folders that you can create: 1) Package 2) Source Folder
1) A package looks like this
To create a package, right click on the folder you want (in my example, I right clicked on Java Resources) --> New --> Package
A pop up window that looks like this should appear
“Source folder:” That is where you choose your project. If you don’t see anything there, click on “Browse…” and find your project. If I click “Browse…” it shows this:
“Name:” is the name you will give your package.
Note: Packages should be within Source Folders. Source Folders are the parent folder.
2) Source Folders look like this:
To create a Source Folder, right click on the folder you want (in my example, I right clicked on Java Resources) --> New --> Source Folder
Under “Project name:” write your project’s name. If you need help, click “Browse…”
Under “Folder name:” write the folder name you want.
Error: If you’re trying to create a “src/main/resources” folder and you’re running into this error “Cannot nest ‘ProjectName’ inside ‘ProjectName’. To enable the nesting exclude ‘main/’ from
Solution:

Related

Maven sources src/main/java are not seen, advices from same topic not working

After creating the Maven Project in Eclipse from maven-archetype-webapp archetype i have the src/main/java not seen in project explorer and advices from same topic here do not work, so it is not a duplicate, because:
in my effective POM already stays :
<sourceDirectory>X:\Programming\workspaceEclipse\TomkatFromArchetype\src\main\java</sourceDirectory>
<scriptSourceDirectory>X:\Programming\workspaceEclipse\TomkatFromArchetype\src\main\scripts</scriptSourceDirectory>
<testSourceDirectory>X:\Programming\workspaceEclipse\TomkatFromArchetype\src\test\java</testSourceDirectory>
<outputDirectory>X:\Programming\workspaceEclipse\TomkatFromArchetype\target\classes</outputDirectory>
<testOutputDirectory>X:\Programming\workspaceEclipse\TomkatFromArchetype\target\test-classes</testOutputDirectory>
what is actualy ok.
But after updating maven, installing, building - whatever, i can not get the damn src/main/java shown in the project explorer and can create new java classes only in src/main/ressources.
I want generally know what is the reason for that behaviour, options for folders in m2E, so i could turn it and configure in different ways manually.
Some more info:
The folder src/main/java is sort of "exist", because if i try to create the folder, it shows that there is one with this name, but is not seen.
Opening project properties (right click) -> java build path ,stays
src/main/java and src/test/java (missing), how can it be?
Solved by looking in the file explorer, is the path realy there.
If it is there,it may be added in project explorer menu. If not - you create it in file explorer and add in project explorer menu
this issue occur into eclipse many time. you need to go to Project property > project facets and click on convert to faceted from.. then apply and ok. after this you can see the src/java/main into your project resources. you can fine image below:
One solution that could work in this scenario(suddenly src main java folder vanishes) is :
delete the project from the workspace. (not from disk)
re-import the project again as Maven project.
This worked in my case.

How project structure is displayed in eclipse, how manage it?

I have Spring-MVC application. Files like: .project, .classpath, I dont keep in repository, I generate it by eclipse during import (eclipse import maven project).
Sometimes in Project Explorer View, project looks like:
And sometimes like below:
Can I manage this, how? I prefere second view, how convert first to second view? On screens are Eclipse Juno and Kepler, both have the same plugins installed (mostly).
In the first screenshot, the SRC/ folder is not a source folder, whereas in the second the folders SRC/main/Java, SRC/main/resources... are sources folder and not the SRC/.A right click on the SRC/ and then Build path, set a source folder should
fix that in the first image. In the second, remove from build path SRC/* and then do as above.

Creating subfolder in Eclipse with folders listed in one line

I created a new webapp project in Maven and noticed that the Java folder was not being created.
So, I created a new folder under src/main/java..The folders are listed in a nested format
ie;
src
main
java
When I noticed few other existing projects, I noticed its in the following structure
src/main/java
src/main/resources
What option needs to be enabled to change the setup?
Package Explorer / View Menu (Ctrl-F10) / Package Presentation... / Hierarchical
If you have the Maven Eclipse plugin, right click on the project and select Maven > Update Project Configuration. Then the source folders are selected and you have the flat structure within your Eclipse project like src/main/java.
Another way (without Maven plugin): Right click on the corresponding src directory and select Build Path > Use as Source Folder

Eclipse Package Declaration Work Around?

The team in which I'm working doesn't use eclipse and wants the src folder to be setup a certain way. I, on the other hand, am using eclipse. They would like the src folder to have, for example, the following structure: src/main/java/com/* However, they would like package declarations to only have com.* in them. If I go to project->properties and in the source tab remove the src folder as a source folder and then _only add the com.* folder as part of the source_ it will show com.* as a package, but I still have to add the whole path from src down in the .java files. Here's an example of what I have to do in order for eclipse to recognize the packages:
package main.java.com.parser;
And what the team wants is to have main and java just be a folder so the package declaration would just be:
package com.parser;
So although the file structure would still be src/main/java/com/parser, the package name would be as stated in the later example.
Is there a way to do this in eclipse? I've seen some people asking similar questions on here, but they seem to have no concern that the whole path must be part of the package declaration.
I would much prefer not to do this this way, but no one else is using eclipse on this project.
Thanks for any help in advance.
In the Package Explorer, right click the src folder and select Build Path > Remove from Build Path
Then find the src/main/java folder, right click java and select Build Path > Use as Source Folder
Basically this tell eclipse that java is the folder which contains the packages and source files.
P.S. This is a very normal project setup for building with Maven. If you download the m2e (Maven to Eclipse) plugins and choose to create a Maven Project (or import from the pom.xml) then Eclipse will automatically know how to correctly locate the source folder.

Eclipse showing package name including directory structure

I have my source in the hierarchy:
folder: src/main/java
package: com.test.serviceImpl
Now eclipse is showing this as src.main.java.com.test.serviceImpl
How can I make it display only com.test.serviceImpl under the directory structure : src/main/java
Note: This is a maven project module imported to eclise. All other modules display properly
If you use m2eclipse and your pom is configured properly, just right-click the project and select Maven > Update Project Configuration.
If that doesn't work, study the Eclipse Maven Console (in the Console view) for errors.
Set src/main/java as a source root directory.
Here's the standard Maven directory structure - like it or not:
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
I had a problem which may be related.
Eclipse started showing folder names as a concatenation of the folder name with other elements. A folder named "x" was being replaced with "xorg.eclipse.jdt.core.javabuilderorg.eclipse.m2e.core.maven2builder"
After exploring various possible explanations and/or solutions, I realized this behavior appeared after my having deleted a project folder without checking the box "Delete project contents on disk (cannot be redone)". When I subsequently re-checked out the code from an SVN repository, a folder named "x" was still on my disk, and so I suspect Eclipse created a longer version of a folder to contain "x" using other elements from the buildSpec.
I fixed the problem by closing Eclipse, deleting both folders ("x" and "xorg.eclipse..."), reopening Eclipse and checking out the project again.
I had the same problem on my Maven project and I solved it just updating my project Maven>Update Project