9

I have converted an existing Java project to a Maven project and Maven builds everything perfectly when using the command line.

When I import the same project into Eclipse and compile (by right-clicking the project -> runs as Maven build, it still compiles without any issue.

However, I am not able to see the source folder. When I check the build path it gives warning - build path entry is missing.

I am not using standard src/main/java since I had a pre-existing folder structure for the project which could not be changed.

Here's my pom (notice the sourceDirectory tag):

<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>
  <groupId>com.mycom</groupId>
  <artifactId>maven</artifactId>
  <version>17.4</version>
  <name>maven</name>
  <properties>
    <projecrt.rootDir>../../java</projecrt.rootDir>
  </properties>
  <build>
    <finalName>re</finalName>
    <sourceDirectory>${projecrt.rootDir}</sourceDirectory>
    <plugins>
      <plugin>    
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>               
            <manifestEntries>
              <Build-Version>${buildversion}</Build-Version>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>  
</project>

and here's my directory structure:

Src/java
----maven/pom.xml
----com/mycom/<...> // application code
zb226
  • 9,586
  • 6
  • 49
  • 79
tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
  • The way you are using the pom eclipse is unable to determine the source folders. You can either add the source folder through the UI manually (right-click -> Build-Path -> Use as source folder) or utilize the maven build helper plugin to add the source folder through maven in a way that eclipse recognizes the source folder correctly. – DrHopfen Jul 09 '18 at 08:06
  • 2
    I strongly recommend to follow conventions ... – khmarbaise Jul 09 '18 at 08:57

2 Answers2

8

You can use this build-helper-maven-plugin

And configure it in your pom file this way

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>some directory</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

and now when you do mvn eclipse:eclipse the custom source folders will get added to the classpath entries in the .project file. So, you wont need to do any manual configuration, your configuration will simply get build at run-time. You can do the same for your test sources as well, if you have your tests in a custom source folder. Hope this helps.

Community
  • 1
  • 1
MithunS
  • 485
  • 7
  • 28
  • 6
    Please please never use `eclipse:eclipse` anymore. This will make m2eclipse useless, which is the best Maven Integration solution for Eclipse. – Robert Scholte Jul 17 '18 at 21:25
  • I am using the same maven plugin to add a generated source directory into the classpath, and I just need to do `Context menu` on the project → `Maven` → `Update project…` to make it work in Eclipse. – vdubus Jul 20 '18 at 08:42
  • Is this solution about adding generated sources? My question is about being able to run/debug/edit the sources that would normally exist under `src/main/java` for standard maven directory structure. But in my case the directory structure is not standard. Will the above solution help in running / debugging / setting breakpoints on my source code in the same way as it does when maven standards are used ( `src/main/java` )? – tryingToLearn Jul 21 '18 at 14:17
  • Yes it would, have you tried this yet ? And what challenges did you face ? – MithunS Jul 21 '18 at 15:22
  • I tried but I didn't see the source folder yet. I will try again after cleaning my workspace. – tryingToLearn Jul 23 '18 at 05:45
  • The eclipse maven integration plugin internally uses `eclipse:eclipse` command. I dont think it does any harm if you do on your own. @RobertScholte – MithunS Jul 23 '18 at 15:34
2

Once your Java project is imported in the Eclipse workspace, you can specify the actual src folder:

Click Open Java perspective Window > Open Perspective > Other... > Java to change to the Java perspective.

  • In Project layout group, change selection to Create separate source and output folders and edit Configure default... to modify Source folder name from "src" to "sources".

https://help.eclipse.org/neon/topic/org.eclipse.jdt.doc.user/gettingStarted/images/qs-OrganizeSources2.png

In the settings of your project (project => property => java build path), you can also change/add src folders:

https://help.eclipse.org/neon/topic/org.eclipse.jdt.doc.user/gettingStarted/images/qs-OrganizeSources3.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • it works, but you will have to repeat that configuration each time you change the maven project and regenerate the eclipse project. It is easyer with the build-helper-maven-plugin. – gdegani Jul 16 '18 at 14:01