Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to have .java files in /src/main/java when compiling with maven?

I tried to convert a project to a maven one... it has its .java files in other location than /src/main/java , when i run maven install, all the files (.hbm , .xml) except those .class occurs in my jar.

This is build part from pom.xml :

<build>
    <resources>
        <resource>
            <directory>${basedir}/src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes> 
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
                <fork>true</fork>
                <compilerVersion>1.5</compilerVersion>
                <compilerArguments>
                    <encoding>UTF-8</encoding>
                </compilerArguments>
            </configuration>
        </plugin>
    </plugins>  
</build>

Using the above code, .class files are missing from my jar... and no error messages are displayed on console, is it necessary to move them to /src/main/java package structure ?

Also, what could be the reason for this behavior , some missing dependencies ?

I did a change moving them to , src/main/java and i got some exceptions on console , but i am still confused if i "must" add them to this "src/main/java" structure...

Please give me an idea about this...

Thanks

like image 376
user398920 Avatar asked Dec 07 '22 02:12

user398920


2 Answers

You can configure the source directory to be the same as the resources like this:

<build>
    <sourceDirectory>${basedir}/src</sourceDirectory>
    <resources>
        <resource>
            <directory>${basedir}/src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
</build>
like image 187
Jörn Horstmann Avatar answered Jan 03 '23 10:01

Jörn Horstmann


It not strictly necessary - its configurable - but Maven comes with a standardized project layout and it is highly recommended to embrace it, like others Maven conventions (don't fight against Maven, adopt it).

I understood that you are migrating from Ant to Maven but you are IMO not on the right path, bending Maven to make it fit in your existing project structure/workflow is just not the recommended approach:

  1. Maven strongly suggests using its conventions (over configuration) to ease things. Not doing so makes things more complicated and generate useless configuration overhead.
  2. When deviating from the defaults, you're kinda on your own.
  3. Not using defaults might result in bad surprises, some (poorly) implemented plugins might be using hard-coded paths (like src/main/java, target/classes, etc) and won't like changing defaults.
  4. etc, etc, etc.

The recommended way would be to transform your existing structure into a maven compatible modular structure and to adopt the standard Maven layout.

Adopting Maven standards and conventions will just make your Maven life easier in the long run and you get a standardized structure, which Maven is much about.

I think enough people wrote similar advices so I won't insist more. But you should listen to these advices (and not insist trying to make Maven fit in your existing structure), especially since you're new to Maven. From my point of view, you're not migrating from Ant to Maven, you're trying to migrate Maven to an Ant build.

See also

  • How to convert from Ant to Maven in 5 minutes
  • Convert Ant Projects into Maven Safely in Five Phases
like image 27
Pascal Thivent Avatar answered Jan 03 '23 10:01

Pascal Thivent