Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse maven build path issue in case of custom sourceDirectory

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
like image 640
tryingToLearn Avatar asked Jul 09 '18 04:07

tryingToLearn


2 Answers

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.

like image 162
MithunS Avatar answered Nov 03 '22 17:11

MithunS


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

like image 34
VonC Avatar answered Nov 03 '22 16:11

VonC