Handling unconventional source directory for a web project in maven - java

I've inherited a web project (Servlets) which is currently build inside eclipse.
I want to add maven around it. But the project's source directory is not following the maven convention. Instead of being inside src/main/java, it's src/package/name/...
I don't want to change anything right now because they are working at full speed towards a milestone. Can I configure maven to accept src/ as the java source directory ? Thanks

Just add this to your pom in the build section.
<sourceDirectory>${basedir}/src</sourceDirectory>
Here's the relevant section of the POM doc on configuring the directories.

Related

Convert Java/Dynamic web project to maven project using Maven Command line

I want to convert a Java/Dynamic web project to a Maven project. I use Eclipse IDE to develop Java applications.
One of the straight forward options available to me is to use 'Convert to Maven project' in Eclipse. But there are situations where I cannot use that option since the Maven plugin doesn't work in some networks like my Work environment.
So I want to know a Maven command (on Command Line) that would help me convert my Eclipse-built Java Web App to a Maven project.
Thank you in advance, Happy learning.
The Eclipse feature "Convert to Maven Project" works on projects that already have an appropriate POM. A project that was initially created by Eclipse doesn't have that POM.
So what you must do, is simply create a POM with packaging type WAR, then put it into the root of the project.
You also have to take care of the source directories. The Maven standard way is to have all sources under these four directories:
src/main/java
src/main/resources
src/test/java
src/test/resources
Eclipse simply stores everything under src. So you either change your file and directory structure, or you change the appropriate <build> parameters in the POM. I suggest the former.
The "Convert to Maven Project" feature does more than adding the Maven nature to the Eclipse build settings. It also creates and configures the Eclipse project meta files (the files .project and .classpath and the folder .settings). Therefore I suggest to delete them first in your project directory, so Eclipse can start on a clean project.
Afterwards you simply can convert your project with the above mentioned feature. It should create the meta files, and - as it is a web project (packaging type WAR) - it also should add the appropriate natures that let Eclipse show the project as a web project.

Eclipse: how to properly integrate a Maven/Ant project into a larger one?

I have a Java project that depends on a 3-rd party component. This component is available both as a jar and as a Maven/Ant project. One option for me is to simply add the jar as a library. However, I prefer to add the source code into my project since I may need to slightly modify their source code to better suit my needs.
What's the right way to do it in Eclipse?
My main project has a simple structure: src/ and lib/. The external component also has a standard structure: src/, test/, build/, target/, pom.xml, build.xml. So do I need to copy piece by piece (like contents of one src/ into the other src/), in which case what goes where? Or do I somehow copy it all at once? Or smth else?
The best way would be if you use maven on your projet for dependency management. This way, if you have the other projects open in eclipse, your project will resolve them as local projects, but if you don't, maven will try to fetch the jars from the configured nexus repository.
This way, you avoid having to manually configure your Eclipse projects. Maven will be able to configure your project anywhere you want to build it, not having to manually configure dependency resolution.
Import both the projects into eclipse. Add the reference of 3pp jar project to your project as a reference by clicking on Add on build path option. While delivering it as output there will be a dependency to the 3pp jar project. So either deliver it as separate jar and add it to classpath while executing your project else you have to copy the entire source files into your project and deliver it test complete jar.
Making a jar will be handled by eclipse itself.

Can files outside of the Maven folder structure get compiled?

We have a Java project that was modified about 2 years ago based on the dates.
The project uses a the Play Framework which as I recalled built and ran just fine back in 2012.
The developer apparently had tried to change the project to be a Maven project but the folder structure is all over the place and not within the Maven src folder structure.
Attempting to run the Play commands does not work on building the project any longer and using the Maven commands does not compile the code.
What occurs is just the packaging of all of the folders and source code into a .jar file.
So the question is 'Can files outside of the Maven folder structure get compiled?' if so how, OR do I need to restructure all the code to be placed into the proper Maven folder structure to try and get this to work again?
Thanks for your time.
Rough view of the folder tree below: Unable to post the POM as it is on another system
Project Name
src
main/java/
trunk
Project Name
... play framework folder structure in here eclipse, modules, precompiled, tmp
conf
lib
Web Content
META-INF
WEB-INF
Yes you can compile files in a non-standard Maven folder structure. Maven natively supports multiple source directories for the purposes of generated sources.
Read the Maven use guide When You Can't Use the Conventions
Using Multiple Source Directories This occurs when you are producing a
single JAR (or other artifact), and have several source directories
with classes you want to include.
This answer shows how to edit the directory structure in Maven by specifying the appropriate properties to override from the superpom.
The Maven pom docs show the build element set mentioned in the link above.
As a side note this answer covers a non standard directory layout for building war.

How to construct maven project from source code

I have compiled sources of java web application. I know that project uses maven, because there is pom.xml files inside .war file.
I want to know is it possible to re create maven project using .war file. I use Java Decompiler to get sources, but i do not know how to combine all folders and .java files as it was in original project.
Is there any tool or howto to do it automaticaly?
Here is sources directory tree structure
For information: I do not want to stole some project or code, it's just my work. There is some web app in our production that was done by other developers in 2007. Now we are supporting this projects, and i don't know why customers do not have sources.
Create a new project from scratch as explained in Maven - Guide to Webapps. Then find the src/main/java directory and create a directory structure that reflects packages of classes in the war. Move the resources to resources folder. Use the command mvn package to recreate the war.
You can follow following steps. If your pom.xml is proper
Create a folder say, project
Copy your source code files with package structure intact to project folder( Note: if subfolder should src /project/src .
Copy pom.xml
In eclipse File->Import->select Maven-> Existing maven projects-> select the folder(project)-> Follow the instruction

m2eclipse buildpath mess up

Being a newbie with maven, I've set up a new project with the archetype "webapp" and try to build from that.
My project structure is as follow : src/main/resources for production classes, and src/main/test for unit tests. Everything is working fine but when i use the m2eclipse menu "update project configuration", the build-path mess up ... It seems to me that eclipse doesn't recognize anymore my packages "com.example". Files are still in src/main/resourceS/com/example, but it says "the resource is not in the build path".
When i try to configure the project build-path, i notice that the parameter "Excluded" is set to ** for both source folders ... If i remove the ** token and set the exclusion filter to "none", errors in files disappear but the package mess up and eclipse want to force the test files in the default package ...
Any help with eclipse build-path and maven much appreciated,
Thanks
My project structure is as follow : src/main/resources for production classes, and src/main/test for unit tests.
This is not correct, sources should go in src/main/java. See Introduction to the Standard Directory Layout for some guidelines about Maven defaults.
Everything is working fine
I highly doubt "everything" is working fine. With your current setup, sources simply won't be compiled.
but when i use the m2eclipse menu "update project configuration", the build-path mess up...
It's your project setup that is messed up. Put sources in src/main/java.
When i try to configure the project build-path, i notice that the parameter "Excluded" is set to ** for both source folders ... If i remove the ** token and set the exclusion filter to "none", errors in files disappear but the package mess up and eclipse want to force the test files in the default package ...
If your maven project is properly set up, m2eclipse will derive a proper eclipse configuration from it. You don't and you shouldn't have to change anything manually.
Your project need to be buildable from the command line, i.e. have a valid pom.xml with packaging war and you need to have WTP and Maven integration for WTP installed to work with the web projects.
Resources are excluded from build path to allow Maven builder filter them. You shouldn't remove that exclusion.
You say that your Java files are located in src/main/resources? This is not the correct place to put your Java classes. The correct path is src/main/java.
Here is the conventions for paths with Maven 2 / 3.

Categories

Resources