How do I import an alternative pom.xml file in m2eclipse - eclipse

The only possible input is a directory where m2eclipse will automatically scan for pom.xml. What if you want to import an alternative file (say abc.xml)? can it be done and how?
Thank you for your time.

No, it's not possible. m2e relies on the presence of a pom.xml. The only way to have a different behaviour in m2e is to use maven profiles. The following will be activated automatically when using m2e :
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
...
</profile>
Your CLI builds will remain unaffected, unless you manually use the m2e profile.

Maven is pretty dependent on the pom.xml keeping it's name; repos, developers and other tools expect it to be there for a maven project.

in the eclipse navigator view, right click on your project and click new->file
click advanced and check off "link to a file in the file system" and specify the path

Related

How can I configure Maven (command line) and the IDE to build in different folders?

I'm often switching between my IDE (running just the unit tests for the code which I'm working on) and the command line (running all unit tests).
This causes problems: Sometimes, mvn clean fails because the IDE is building the code. Or I get weird errors in the IDE about missing classes because the IDE is reading files which Maven has modified.
Also, I can't continue working in the IDE why Maven builds in the background.
How can I configure my IDE or Maven to use different build folders?
Use profiles and this code in your (parent) POM:
<project>
...
<build>
<outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
<testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
</build>
<properties>
<target.dir>target</target.dir>
</properties>
<profiles>
<profile>
<id>ide-folders</id>
<properties>
<target.dir>target-ide</target.dir>
</properties>
</profile>
</profiles>
...
Configure the project in your IDE to enable the profile target-ide when building. Now Maven and the IDE use different folders.
Alternatively, you could enable this profile in your settings.xml and make the folder target-ide the default. This way, you wouldn't have to modify the settings of many projects in your IDE. It would even work on your CI build server (it would build into target-ide but who cares? And if you cared enough, you can enable the profile there as well).
Note: This is based on a blog-post on jroller.com which is down. The Internet Archive has a copy: https://web.archive.org/web/20150527111413/http://www.jroller.com/eu/entry/configuring_separate_maven_output_folders

Activating a settings.xml Defined Maven Profile From POM File

Our enterprise team maintains a settings.xml file that defines various profiles that development projects can use with Maven to build their artifacts and install them in a repository. The expectation is that each project in Jenkins would define a -P parameter with the desired profile specified.
Here is my issue: The profile I need to use specifies a repository that is normally not visible to my project (a staged release repository), so anything in that repository cannot be depended upon without that profile being active. I need a way to activate that profile defined in settings.xml in my project's pom.xml file. Otherwise, when looking at my project in Eclipse, errors are shown in the pom.xml that dependencies cannot be resolved because the profile isn't active to make the repository visible.
Is there a way to activate a settings.xml defined profile in a pom.xml file? Alternatively, is there a way to tell Eclipse to always activate a profile for a particular project?
You can use the activateByDefault tag to specify a default profile to use:
<profile>
<id>ProfileToActivate</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
After more research, I found a project specific Maven property in Eclipse where I can set the active profiles. You can set this by right-clicking on the project, select "Properties", and select "Maven". You are presented with a text entry box for "Active Maven Profiles (comma separated):".
That solved my problem.
I was looking for a more global solution in Eclipse rather than at the project level. But this seems to work.

Maven profiles are not considered in Eclipse

I have three profiles defined in my pom.xml:
<profiles>
<profile>
<id>ABC</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<url.base>http://server1.de</url.base>
<url.searchevse>/search</url.searchevse>
<url.reservation>/reservation</url.reservation>
<url.cancelation>/reservation/cancel</url.cancelation>
<xxx.devmode>false</xxx.devmode>
</properties>
</profile>
<profile>
<id>XYZ</id>
<properties>
<url.base>http://server2.de</url.base>
<url.searchevse>/cns/search</url.searchevse>
<url.reservation>/cns/reservation</url.reservation>
<url.cancelation>/cns/cancel_reservation</url.cancelation>
<xxx.devmode>false</xxx.devmode>
</properties>
</profile>
<profile>
<id>DEVELOPMENT</id>
<properties>
<url.base>http://localhost/noservices</url.base>
<url.searchevse>/no/search</url.searchevse>
<url.reservation>/no/reservation</url.reservation>
<url.cancelation>/no/cancel_reservation</url.cancelation>
<xxx.devmode>true</xxx.devmode>
</properties>
</profile>
</profiles>
In Eclipse I have a Run Configuration
clean install XYZ
and I tried both using -PXYZ (and -P XYZ) in the Goals field as well as
clean install
in the Goals field and XYZ in the Profiles field.
The problem:
The defined profile is never used.
Inserting the active profile under Properties-->Maven-->Active Maven Profiles doesn't work (or do I have to use a special syntax, e.g. no spaces after a comma or so).
Right click your project in the Project Explorer then go to
Properties --> Maven --> Active Maven Profiles
and type in only the profile name that you want to run.
If you want to run your ABC profile, type in ABC in the Active Maven Profile input box.
The description(separated by commas) given there is a bit confusing.
Once you define your profile name or id in the input box. You can clean and run your project on your server.
By doing so, your mentioned active maven profile will be run.
verify that plugin execution id are not equal in different profiles
For those who still have problems. I am not really used to Eclipse nor STS but I actually use Spring Tool Suite Version: 4.7.1.RELEASE. I think the Active Maven Profile input box. may be named Write here your already activated Maven Profile :) as I think its purpose is to select an activated profile to run.
In my case, what I did was :
edit my Maven settings.xml (maven folder, conf folder) file to activate the desired profile with <activeProfile>MyProfileId</activeProfile> in the activeProfiles section. The one thing I really appreciate is that it accepts profile even defined in your project pom.xml and that is great for peoject with sub modules. If you have time, read this https://maven.apache.org/guides/introduction/introduction-to-profiles.html
Then add your profile to Active Maven Profile input box (Ctrl+Alt+P or right click on your project, Maven, Select Maven profiles). You can also deactivate a profile with ! or - before a profile name.
You might need to restart Eclipse.
The first step is only to activate your profile. So if you have any way to achieve this, feel free to do as so.
In Eclipse 4.20 you can right click on project -> Maven -> Select Maven Profiles...

How properly import maven projects to eclipse?

I have complex Maven project, which has a parent folder with parent pom.xml and some subfolders for subprojects, each with its own pom.xml.
When I checkout the parent folder from a SVN repository 'As maven projects', it correctly appears in the workspace each as separate project.
But now I made a checkout of the parent folder from other branch of project. I want to add this parent folder in the same manner to the current workspace. So I just select Import > Maven project, get the same dialogs as due checkout from svn, Eclipse finds all pom files with proper hierarchy and ask me give other name to parent project, cause the same name alredy exists (trunc version of project), I give it other name. But after import I get only parent folder as maven project in eclipse, all other subprojects are simple located under parent project as its subfolders.
So, how can I import such project properlty? I just want all subprojects created as maven projects too.
Eclipse doesn't allow one project to be imported more than once, in your case from trunk and a branch. This article shows how you can bypass this limitation with a custom maven profile. Basically, the steps are:
Add the following profile to your parent pom.xml
<profiles>
<!-- Specific profile used to append a string to project name -->
<profile>
<id>append-to-project-name</id>
<activation>
<property>
<name>append.to.project.name</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<projectNameTemplate>
[artifactId]-${append.to.project.name}
</projectNameTemplate>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Before importing the project to Eclipse, let maven generate the project settings:
mvn eclipse:eclipse -Dappend.to.project.name=[your-branch-name]
Import the project to Eclipse as an existing project (not a maven project).
This should solve the naming issue.
As for import of child projects: I also have a parent maven project with child subprojects and use the m2e plugin for Ecplise. When I select Import > Existing Maven Project, I can check both the parent and children. After import, I have both the parent project and each child imported independently. Screens:
So I hope this combined with the naming solution above should solve your problem.
For importing complex (or indeed any) Maven projects from an SCM (be it SVN, Git or any other SCM) I use the following approach:
File->Import->Check out Maven Projects from SCM
This works best if no IDE specific files have been committed to the SCM. It will import all nested projects or you can choose which projects to import. It relies entirely on the Maven settings for determining the nature of the project being created so that, assuming the POM files are correct, you wind up with everything configured just right.
You may need to install add-ons so that m2eclipse can access the SCM of your choice.
I guess there might be some workspace issue. You might have checked out the project in workspace itself(not sure so pardon for that).
What you can do is, try checking out your project in some other drive and then import the parent project. You will again see all POMs aligned in hierarchical way and then i suppose, you won't be asked for entering name for parent project and this will do the trick.
Delete the projects that you had checked out as Maven projects, then check out the parent project simply (not as Maven project), then Import the child modules as Existing Maven projects. Make sure they are listed as child modules in the parent POM.

Get source jar files attached to Eclipse for Maven-managed dependencies

I am using Maven (and the Maven Eclipse Integration) to manage the dependencies for my Java projects in Eclipse. The automatic download feature for JAR files from the Maven repositories is a real time saver. Unfortunately, it does not include API documentation and source code.
How can I set up Maven to automatically also get the source and javadoc attachments and register them properly with Eclipse?
I am sure m2eclipse Maven plugin for Eclipse - the other way around - can do that. You can configure it to download both the source files and javadoc automatically for you.
This is achieved by going into Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options.
mvn eclipse:eclipse -DdownloadSources=true
or
mvn eclipse:eclipse -DdownloadJavadocs=true
or you can add both flags, as Spencer K points out.
Additionally, the =true portion is not required, so you can use
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs
The other answers on this work, but if you want to avoid having to remember command line arguments, you can also just add to the downloadSources and downloadJavadocs config to the maven-eclipse-plugin section of your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I prefer not to put source/Javadoc download settings into the project pom.xml file as I feel these are user preferences, not project properties. Instead, I place them in a profile in my settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>sources-and-javadocs</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sources-and-javadocs</activeProfile>
</activeProfiles>
</settings>
Right click on project -> maven -> download sources
If the source jars are in the local repository and you are using Eclipses maven support the sources are getting automatically attached. You can run mvn dependency:sources to download all source jars for a given project. Not sure how to do the same with the documentation though.
If you are using meclipse do
window --> maven --> Download Artifact Sources (select check)
(If you still get attach source window, then click on attach file button and close the attach source window. The next time you try to see the source it will open the correct source)
There is also a similiar question that answers this and includes example pom settings.
I tried windows->pref..->Maven But it was not working out. Hence I created a new class path with command mvn eclipse:eclipse -DdownloadSources=true and refreshed the workspace once. voila.. Sources were attached.
Source jar's entry is available in class path. Hence new build solved the problem...
in my version of Eclipse helios with m2Eclipse there is no
window --> maven --> Download Artifact Sources (select check)
Under window is only "new window", "new editor" "open perspective" etc.
If you right click on your project, then chose maven--> download sources
Nothing happens. no sources get downloaded, no pom files get updated, no window pops up asking which sources.
Doing mvn xxx outside of eclipse is dangerous - some commands dont work with m2ecilpse - I did that once and lost the entire project, had to reinstall eclipse and start from scratch.
Im still looking for a way to get ecilpse and maven to find and use the source of external jars like servlet-api.
Changing pom for maven-eclipse-plugin to include source/javadoc just apply for new dependencies being added to pom. If we need to apply for existing dependencies, we must run mvn dependency:sources. I checked this.
Checking download source/javadoc in Eclipse-Maven preference, sometimes is not enough. In the event maven failed to download them for some reason (a network blackout?), maven creates some *.lastUpdated files, then will never download again. My empirical solution was to delete the artifact directory from .m2/repository, and restart the eclipse workspace with download source/javadoc checked and update projects at startup checked as well.
After the workspace has been restarted, maybe some projects can be marked in error, while eclipse progress is downloading, then any error will be cleared.
Maybe this procedure is not so "scientific", but for me did succeded.
I've added the pom configuration to the maven-eclipse plugin to download source and javadocs, but I figure/hope that will happen for new dependencies, not existing ones.
For existing dependencies, I browsed in package explorer down to the "Maven Dependencies" and right-clicked on commons-lang-2.5.jar, selected Maven | Download Sources and... nothing appeared to happen (no progress bar or indication that it was doing anything). It did, however, download as I'm able to jump to source in commons-lang now.
overthink suggested using the setup in the pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plgins>
</build>
...
First i thought this still won't attach the javadoc and sources (as i tried unsuccessfully with that -DdownloadSources option before).
But surprise - the .classpath file IS getting its sources and javadoc attached when using the POM variant!
For Indigo (and probably Helios) the checkboxes mentioned above are located here:
Window -> Preferences -> Maven
I had a similar problem, and the solution that worked best for me was to include the source in the same jar as the compiled code (so a given directory in the jar would include both Foo.java and Foo.class). Eclipse automatically associates the source with the compiled code, and automatically provides the JavaDoc from the source. Obviously, that's only helpful if you control the artifact.
After Setting the Properties either at Project Level or User Properties level,
Please do a Maven -> Update Project (Force Update). It downloads the sources
A Small addition to the answer, if your project is not a maven project still you can get the source code of the jars, by using this plugin provided in eclipse
Java Source Attacher