I have one maven project with pom.xml.
This project refers another 2 projects.
These 2 projects have their own pom.xml in their respective folders.
So when I build base pom.xml other 2 projects also get built.
Now here is what I did
a) Build base project (This built 2 subprojects as well).
b) Used mvn eclipse:eclipse to export base project.
c) Imported base project into eclipse using Eclipse menu File-> Import existing Maven project.
After importing it created 3 different projects in Eclipse.
As methods for 3 projects get called from each other it gives many errors.
What I want is after importing it should create single project and all the code will be in a single project.
Is there any way to do this?
You could make a multi-module project. This will allow you to define a top-level project and the structure of it's dependencies. It will likely still require some refactoring, but it should be relatively modest.
Maven website: Guide to Working with Multiple Modules
A third party guide
Simple answer is NO.
Unless you manually merge/refactor these 3 projects into one.
pom.xml is per project, here there are 3 poms.
Related
I've got a couple of java projects that depend on the same piece of code. Lets call them:
Project A
Project B
ComponentX.java
The projects are set up like this:
Project A > ComponentX.java
Project B > ComponentX.JAR
This set up where Project B contains ComponentX's jar file is inconvenient. Any time I make a change to ComponentX.java, I need to remember to export the Jar to Project B. It also makes debugging Project B difficult. There has to be a better way, I just don't know what to google. I'm using Eclipse. I use Maven so any solution using that would be Ok, although I'm hoping that there is a simple Eclipse based solution.
I'm guessing I'll need to move componentX into its own project and somehow make the other two depend on it.
You most likely want to create a new library project that will be used by both Project A and Project B as a standard Maven <dependency>.
There are few guides available online. If your projects are all part of the same multi-module Maven build you can take a look at Creating a Multi Module Project guide on spring.io.
So I had a lightbulb moment. Any maven project can be imported into any other maven project the exact same way you'd import any other dependency. Seems obvious now but I've only ever imported libraries other people wrote.
So if you've got three projects:
WebApp
DesktopApp
Dependency
Where both WebApp and Desktop need to reference Dependency. This could also apply in a case where there is repeated code in two projects, just pull that out into it's own project and remove it from both.
Simply make Dependency project a Maven project by right clicking > Configure > Convert to Maven Project. You'll get a .pom file with something like this at the top:
<groupId>Dependency</groupId>
<artifactId>Dependency</artifactId>
<version>0.0.1-SNAPSHOT</version>
Then all you have to do is add that as a dependency to any other project's .pom that needs access to it:
<dependency>
<groupId>Dependency</groupId>
<artifactId>Dependency</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
That's it
Currently, I have a maven project that has a server running (using Jersey REST API). I also have a java project, I need to move all the contents of the java project into the maven project. The maven project is a subset of the java project. However, the maven project only displays the parts of the java project. However, I want a project that allows me to use maven and displays all of the other details from the java project.
I would've copied and pasted however I'm using git so I want to also preserve history.
I was thinking it would be easier to nest the maven project inside the java project but I don't know if that's possible.
Here's a picture of my package explorer to help explain everything.
Package explorer showing the maven project being a subset of the java project.
What I've tried is converting the java project into a maven project and then updating the pom.xml but then it doesn't link to the web.xml. Also, it tries to run the server with the name of the project name TeamProject. When infact it should run the url with the name client_server
I was considering just copying and pasting all the code into the maven project (from the Teamproject java project).
Actually nesting the java project inside the Maven project makes more sense, as it is the purpose of Maven to handle a project lifecycle. (also by default Maven will look for sources in the src/ folder which should ease the task of putting your Java project inside Maven's hands)
There are several possibilities I would see:
Copy your java project in the src/ of your client project and update maven accordingly (within the pom.xml)
Make your Java project a Maven project and aggregate the two projects in a parent pom (see Multi module maven project example)
Make your Java project a Maven project, and decide of a "Master" project between it and the client and compose one with the other (not sure that's a great solution)
Nesting your Maven project inside the Java project would not be so great because Maven could only handle the client and not your Java project, and then you'd miss on numerous functionalities offered by Maven (just look at how simple it is to get dependencies compared to downloading a jar and including it on the build path manually)
I have 3 projects, A->B->C in that dependency order. Currently everytime I make a change to B or C I have to go to the directory and do a mvn clean install in order to install it into the local repository. It is troublesome if I have to do this every time the projects updates.
How can I do it such that every time I do a mvn clean package on A, it will automatically build and install my dependent projects B and C into the local repository?
Create a parent project for all your projects A,B,C and then add all your child project on the parent pom.xml file something like this
<modules>
<module>A</module>
<module>B</module>
<module>C</module>
</modules>
Its called maven multi module project mentioned by #khmarbaise
Here are some example for this
How do I create a multi-module project in Eclipse?
Maven Multi module tutorial
Guide to Working with Multiple Modules
By use of multi module project you will get plenty of benefits like
Anytime you can add any new project with all of the current project
Separation of project is good for code cleanup
You can build Single project or You can build all project in one go.
Duplicacy of jar can be easily ignore .
Maven take care of the build order for you.
One single Jenkins job to build everything.
Plenty of other benefits.But remember if there will some pros then cons also there,its totally now what you want to use .
You can follow the solution I provided to the question Maven 2 Projects, since it is the pattern I usually use when building project with a certain complexity.
Summarizing you would have to create a main Maven project which has three submodules, say master, platform and parent.
The main project has simply the order in which the other projects will be evaluated by Maven
The master pom contains the list of project to be built and their order (aka Reactor order)
The platform pom contains all information about your platform, like JDK version, maven plugin versions, encoding and so on.
The parent pom has the platform pom as a parent and contains all global GAV information about the dependencies you are going to use in your project (Spring, CXF, junit, log4j etc.)
I have a project that Contains a bunch of java packages that I have recently understood are needed in a new project that I'm going to develop. I want these two projects to have the same code base so I don't have to update the common libraries in both. How can I achieve this in Exlipse?
my current set up is like this
Project1
CommonPackage
SpecifictProjectCode1
Project2
SpecifictProjectCode2
I want the following, I think:
Project1
CommonPackage
SpecifictProjectCode1
Project2
CommonPackage
SpecifictProjectCode2
CommonCodeProject
What is the best way to achieve this I understand I could extract a jar file or and include it in both project but I want to be able to debug the code and I also want to keep the code editable. Is it perhaps better to extract the code to a separate project and how do I go about doing that?
Any help is appreciated.
You can have three different projects in Eclipse: CommonCodeProject, Project1 and Project2. Then, Project1 and Project2 would have a dependency on CommonCodeProject.
To add a dependency on a project in Eclipse, go to Java Build Path and add the CommonCodeProject in the Projects tab.
Create 3 distinct projects:
Project1
Project2
CommonCodeProject
Go into the eclipse properties of Project1 > java build path >
choose the projects tab and add CommonCodeProject project to the dependencies
do the same for Project2
both project should now have as dependency your CommonCodeProject.
YES. Create third project(CommonCodeProject) as Java project and reference that new project in first two projects by navigation below:
Project1 -->Properties-->Java Build Path -->projects tab-> Add -->CommonCodeProject
Project2 -->Properties-->Java Build Path -->projects tab-> Add -->CommonCodeProject
This should serve your need.
if you would not be editing the common code then make it one project and import as a jar in the other two. or even if you are editing have 3 separate projects and build common code to create a jar which is used in other two projects
Aside from native Eclipse (as outlined in the other answers), I would use Maven+Eclipse with the m2e plugin (download this using the Marketplace). Depending on the complexity of your project, and it's application, you could even go as far as hosting something like Sonatype's Nexus with your common code deployed to as repository - this might be overkill though.
I'm using eclipse with the m2eclipse plugin now I just want to resolve a - imho - easy problem: I've got two maven projects, I want to add project A as dependency to project B.
Well how do I achieve this in a manner way? If I add the project A to the build path of project B eclipse recognizes the classes but this project isn't resolved by eclipse on build time.
I got it working by installing project A to my local repo and adding this as dependency to my pom. This works but is cumbersome because I always have to install a new version of project A when something changed.
Shouldn't the plugin handle such a situation for me?
Providing that you have both maven projects open in your workbench then make sure you have the "Enable Workspace Resolution" option enabled in the Maven context menu.
Yes Eclipse handle this situations.
You can add both the Projects A and B in the same work-space.
I could help you creating a simple work-space from the scratch.
I am assuming that you have already installed the MAVEN plugin M2Eclipse for Eclipse.
Start a new Eclipse in a blank Work-space
Right click on Project Explorer --> Go to Import dialog and add a Maven Module.
Locate the POM directory and add that directory.
It will list all the projects in all the sub folders.
Add as many as Maven Based modules in a single work-space.
By doing this you dont need to install the dependencies. Any change will be reflected on the derived module.
Hope that will help you.
The way you did it is correct, because it assumes that project B will be using the dependency of project A that will be in the artifactory, so you can develop both independently.
And anyway, for the project A, if you are using maven, don't you use maven clean install for compiling and deploying? That way you are sure you always have the latest version
The other option is, in case both of the are more dependent of each other, you should consider make one of them as a module of the other, or maybe make a project C that contains both modules, but that would mean both of them are part of the same project (like an ear containing two jars), depends on the situation