views:

68

answers:

5

Hi ,

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

A: 

By far the easiest solution (if possible in your circumstances) is to move the code to /src/main/java, since that is where maven expects it. It that is not possible, you at least need to tell maven where to find the sources. From your POM I don't see anywhere where you did that.

Thomas Lötzer
Thank you very much for posting , i don't know where in pom.xml file to change the default , source build from src/main/java to /src how is done right now... i only changed in "<directory>${basedir}/src</directory>" , i believe that this is not enough :(...
A: 

If you want to use Maven, the path of least resistance is adhering to the Maven conventions. Surely you can have a different source path, but it is definitely more complicated (it seems that the only way is to pass the source directory as a compiler argument), and what do you gain? I believe moving your source directory is much easier.

Péter Török
Thank you very much for posting , i don't know where in pom.xml file to change the default , source build from src/main/java to /src how is done right now... i only changed in "<directory>${basedir}/src</directory>" , i believe that this is not enough :(
@user, check out the link I provided, there you see examples of how to pass arguments directly to the compiler. I have not tried this though (I prefer to spend my time writing code instead :-)... but if you _really_ want to go your own way, you could experiment with it.
Péter Török
Thanks a lot for helping, it worked :).
+1  A: 

Have a look at the Super POM. That is the place you get the 'convention' from. You might have to change some settings in your project POM if you have a differnet project layout but it is no good advice as mentioned here before.

K. Claszen
Thanks for posting, i will have a look... to know how to change the default "/src/main/java" to "/src" in cause that renaming structure to /src/main/java will not be a suitable solution ....
<build><sourceDirectory>${project.basedir}/src</sourceDirectory></build> is the place to change.
K. Claszen
Thanks a lot for helping, it worked :).
+3  A: 

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>
Jörn Horstmann
Thanks a lot for helping, it worked :).
If this is the answer that helped you, you should approve it (flag it on the left side). If you show that you accept answers, this will stimulate others to answer your questions in the future (See Accept Rate)
Jan
A: 

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

Pascal Thivent