Activating a settings.xml Defined Maven Profile From POM File - eclipse

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.

Related

Maven not storing artifacts in the directory specified by localRepository in settings.xml

I am using apache-maven-3.2.3
It is located at C:\java\apache-maven-3.2.3
The settings.xml file is located C:\java\apache-maven-3.2.3\conf\settings.xml
Inside that settings.xml file, I have the following defined:
<localRepository>C:/java/maven_repo</localRepository>
I have windows environment variables defined:
M2_HOME=C:\java\apache-maven-3.2.3
M2=%M2_HOME%\bin
PATH environment variable includes %M2%
I have configures my STS (Eclipse) to use the external Maven installation at C:\java\apache-maven-3.2.3
My STS Maven-->Installations shows that the global settings are: C:\java\apache-maven-3.2.3\conf\settings.xml
My STS Maven-->User Settings shows that the local repository from merged and global settings is: C:\java\maven_repo
There is NO settings.xml at C:/Users/[myuser]/.m2
In fact, I remove the entire .m2 folder at C:/Users/[myuser]/.m2 to be sure
However, When I do a Maven update through STS, it is still downloading artifacts to the default .m2 directory at: C:/Users/[myuser]/.m2/repository
My Eclipse project is also showing the Maven Dependencies are found in C:/Users/[myuser]/.m2/repository
Can anyone tell me why? What have I done wrong? Why wont it use the local repository at c:/java/maven_repo ?
Normally, I will change the default local repository folder from default .m2 to another more meaningful name, for example, maven-repo.
Find {M2_HOME}\conf\setting.xml, update localRepository to something else.
<settings>
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:/maven_repo</localRepository>
Done, your new Maven local repository is now changed to D:/maven_repo.
The only way I have managed to get this to work in Eclipse is to also set the user settings file in Window-->Preferences-->Maven-->User Settings to the same as the global settings: C:\java\apache-maven-3.2.3\conf\settings.xml
But I would imagine that leaving that field blank should take the settings from the global settings.xml, but for some reason it doesn't. So if I leave that user settings field blank, the localRepositorty from global settings.xml does not apply.
However, if I run Maven outside eclipse, it works as expected.

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...

Failed to resolve version for org.apache.maven.archetypes

I have configured maven3.0.3 in my local machine. Have installed m2e eclipse plugin.
But when i try to create a new maven project using maven-archetype-webapp, i get the following exception.
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.
Could not resolve artifact org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE
Failed to resolve version for org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype- webapp/maven-metadata.xml in local ([HOME]/.m2/repository)
Failed to resolve version for org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype- webapp/maven-metadata.xml in local ([HOME]/.m2/repository)
I do some processing behind a proxy and the proxy configurations are updated in {HOME}/.m2/settings.xml and M2_HOME/conf/settings.xml.
The archetype generate command works fine in command line. It downloaded the dependencies through proxy.
Any help is greatly appreciated.
Edit 05-10-2012
While creating a new Maven Web project in eclipse, the archetype "maven-archetype-webapp" version is displayed as RELEASE. Is this in anyway linked?
I had the same problem. I fixed it by adding the maven archetype catalog to eclipse. Steps are provided below:
Open Window > Preferences
Open Maven > Archetypes
Click 'Add Remote Catalog' and add the following:
Catalog File: https://repo1.maven.org/maven2/archetype-catalog.xml
Description: maven catalog
I found the following tutorial very useful.
Step1: The maven command used to create the web app:
mvn archetype:generate -DgroupId=test.aasweb -DartifactId=TestWebApp -DarchetypeArtifactId=maven-archetype-webapp
Step2: The following entry was added onto the project's pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>1.5</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<classpathContainers>
<classpathContainer>
org.eclipse.jst.j2ee.internal.web.container
</classpathContainer>
<classpathContainer>
org.eclipse.jst.j2ee.internal.module.container
</classpathContainer>
/classpathContainers>
<additionalProjectFacets>
<jst.web>2.5</jst.web>
<jst.jsf>1.2</jst.jsf>
</additionalProjectFacets>
</configuration>
</plugin>
Step3: Run the maven command to convert into eclipse project format.
mvn eclipse:clean eclipse:eclipse
Step4: Import the project onto eclipse as Existing Maven project.
If you are using eclipse, you can follow the steps here (maven in 5 min not working) for getting your proxy information. Once done follow the steps below:
Go to Maven installation folder C:\apache-maven-3.1.0\conf\
Copy settings.xml to C:\Users\[UserFolder]\.m2
Modify the proxy in settings.xml based on the info that you get from the above link.
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>your proxy</host>
<port>your port</port>
</proxy>
Open eclipse
Go to: Windows > Preferences > Maven > User Settings
Browse the settings.xml from .m2 folder
Click Update Settings
Click Reindex
Apply the changes and Click OK
You can now try to create Maven Project in Eclipse
Read carefully about the reason.
"Failed to resolve version for org.apache.maven.archetypes:maven-archetype- webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype- webapp/maven-metadata.xml in local"
So all you need to do is download the maven-metadata.xml
to your {HOME}.m2\repository
That's it.
You do need to have a settings.xml linked under user settings (Located in preferences under maven)
But, if that doesn't fix it, like it didn't for many of you. You also have to delete the directory:
.m2/repository/org/apache/maven/archetypes/maven-archetype-quickstart
then quit eclipse and try again.
This is what solved my problem.
Go to Windows-> Preference-> Maven -> User settings
Select settings.xml of Maven
Restart Eclipse
I had a similar problem building from just command line Maven. I eventually got past that error by adding -U to the maven arguments.
Depending on how you have your source repository configuration set up in your settings.xml, sometimes Maven fails to download a particular artifact, so it assumes that the artifact can't be downloaded, even if you change some settings that would give Maven visibility to the artifact if it just tried. -U forces Maven to look again.
Now you need to make sure that the artifact Maven is looking for is in at least one of the repositories that is referenced by your settings.xml. To know for sure, run
mvn help:effective-settings
from the directory of the module you are trying to build. That should give you, among other things, a complete list of the repositories you Maven is using to look for the artifact.
I too had same problem but after searching solved this. go to menu --> window-->preferences-->maven-->Installations-->add--> in place of installation home add path to the directory in which you installed maven-->finish-->check the box of newly added content-->apply-->ok. now create new maven project but remember try with different group id and artifact id.
The right way to solve my problem are as followed. Hope to be helpful to others.
the errors informations.
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories. Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0
Delete the maven-archetype-webapp:1.0 in the directory ~/.m2/repository/org/Apache/maven/archetypes
Download the maven-archetype-webapp:1.0 and the maven-archetype-webapp-1.0.pom from
http://maven.ibiblio.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/
execute mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-quickstart -Dversion=1.1 -Dpackaging=jar -Dfile=此处填maven-archetype-webapp-1.0的路径.
try to establish a maven project of webapp to test whether the problem has solved.
Create New User Environment Variables:
MAVEN_HOME=D:\apache-maven-3.5.3
MAVEN=D:\apache-maven-3.5.3\bin
MAVEN_OPTS=-Xms256m -Xmx512m
Appened below in Path variable (System Variable):
;D:\apache-maven-3.5.3\bin;
This worked for me:- navigate to windows-> preferences-> maven
and check the "download artifacts sources" and click apply.
No need to do all above lengthy steps.
Simply delete c:\Users\.m2\Repository\org folder
Maven will automatically downloads what it needs
I simple use below steps:
Create Maven project -> check checkobox -> "Create a simple project (skip archetype selection)"
It works for me
I downloaded the jar and pom from:
here
and put them here:
\.m2\repository\org\apache\maven\archetypes\maven-archetype-quickstart\1.1\
I also got same error ....And i found that my internet connection is closed so eclipse is unable to download repositories for webapps archetypes and when i got internet connection i just created again maven project with webapps archetypes and my eclipse downloaded repositories and its done...
This is 100% working:
Delete the .m2 folder which is present in your Desktop>User
connect your pc to the Internet
Create your maven project again
Thats it, hope it helps
Try , It worked for = I just removed "archetypes" folder from below location
C:\Users\Lenovo.m2\repository\org\apache\maven
But you may change following for experiment - download latest binary zip of Maven, add to you C:\ drive and change following....
Change Proxy
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>10.23.73.253</host>
<port>8080</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
I had the same problem i solved it by only adding remote catalog
in eclipse go to Window -> Preferences ->Maven ->Archetypes ->click on add remote Catalog then a window will open in that paste
http://repo.maven.apache.org/maven2/archetype-catalog.xml
in that catalog file then hit ok restart eclipse now all working fine
The problem may also come from that you haven't set MAVEN_HOME environment variable. So the Maven embedded in Eclipse can't do its job to download the archetype.Check if that variable is set upfront.

Eclipse doesn't find source attachements

When opening a class included through a Maven dependency, Eclipse tells me that the containing .jar file has got no source attachment.
But in the directory in which the .jar file resides, there is also as corresponding -sources.jar file.
Do you have any hints regarding the solution of this problem?
Is there a way to tell Eclipse where it should automatically look for the source attachment?
Edit: In the Eclipse .classpath file, the sourcepath value is set for external archives, but not for ours. But both jars and sources file reside in the same repo.
The property -Declipse.useProjectReferences=false is also set.
The problem was that there were already sources.jar-not-available files for certain projects in my local repository.
I've deleted them and now the attachment of source files is working. Obviously Eclipse respectively the Maven Eclipse plugin doesn't look for source attachments as long as these marker files exist.
If it hadn't been for the comment from K. Claszen I'd have never came up with this solution.
Check that you have downloadSources in the maven-eclipse-plugin section of your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
...
</configuration>
</plugin>
If you are using m2eclipse plugin for integrating Maven projects in Eclipse, you might need to enable downloading sources and javadocs in m2eclipse preferences:
Window > Preferences > Maven > Download Artifact Sources
For me the file was in place in local repo and eclipse (4.5) kept complaining there is no associated source jar (sources.jar-not-available did not existed). I ended up removing the directory from local repo, letting eclipse to download the sources again repolulating the repo, running mvn eclipse:eclipse and refreshing the project.
For me all these steps was necessary to get this working.
For those who are still having trouble downloading sources for your Maven dependencies (like me), you can try this alternate method:
Find the particular Maven dependency you wished to inspect Java source code for. You need to have the Maven artifact name.
Next, navigate to the Maven repository server where you download the Maven dependency from. For example, I have spring-security-core-5.5.1.jar as my Maven dependency. The repository host site is from mvnrepository.com, in which the repository server is from https://repo1.maven.org/maven2/.
Navigate to mvnrepository.com and search for your Maven dependency artifact. In my case, it would be spring-security-core, and I located the artifact information page here.
On this page, in the table row labeled Files, you want to click on View All, on the right of the JAR link with how many kilobytes displayed to the side. (Underlined)
Once you're viewing the list of all JARs provided with the Maven dependency, click and download the one that says, [artifact name]-[artifact version]-sources.jar. In my case, it's spring-security-core-5.5.1-sources.jar.
Once downloaded, on your local machine, use your File Explorer to navigate to the folder containing the artifact JAR which you have trouble downloading the source codes for. In my case, it would be $MAVEN_HOME/repository/org/springframework/security/spring-security-core/5.5.1.
Move the [artifact name]-[artifact version]-sources.jar into this folder.
Go back into Eclipse, and try attaching the sources. You should be able to pick up the ...-sources.jar file upon attaching it.
After attaching it, you will then be able to see the source codes for your Maven dependency.
Right click at project, select Maven, select Download Sources.
I download a maven project from online,use mvn eclipse:eclipse converted to eclipse project then I occurred the problem.
solution is remove all external lib that start with 'M2_REPO',it's work for me.

m2eclipse not finding maven dependencies, artifacts not found

I'm using m2eclipse as my maven 2 plugin for eclipse. I'm brand new to maven, so my mistake might be simple, although searching has not yielded any solutions for me. I can run maven from the command line and it build successfully. However if I import as an existing maven project, or use mvn eclipse:eclipse and then import I get the artifacts not found in my POM file resulting in no maven dependencies being loaded at all.
What's odd is that I have 2 projects, both children to a parent pom packaged project. One child loads all my maven dependencies while the other cannot find any and says my pom file is missing artifacts. Again, I can install and package them all just fine from the command line.
I even tried loading my projects into netbeans which worked flawlessly, however I am required to use eclipse.
Any ideas on how to fix this?
Update
Directory structure, pom files layout:
--main
--pom.xml (is a pom package that is the parent to two other projects)
--ProjectA
--pom.xml
--ProjectB
--pom.xml
Main is the parent project of both ProjectA and ProjectB. ProjectB has ProjectA as a dependency. Apprently m2eclipse is not happy about that depedency. As soon as I remove the dependency of ProjectA from ProjectB's pom file all the maven dependencies are found by eclipse.
- com.company.myproj:app:6.6.0:jar Missing: ---------- 1) com.company.myproj:main:pom:${myproj.version} ---------- 1 required artifact is missing. for artifact: com.company.myproj:main:pom:$
{myproj.version} from the specified remote repositories: apache-incubating (http://people.apache.org/repo/m2-incubating-repository/, releases=true, snapshots=true), central (http://
repo1.maven.org/maven2, releases=true, snapshots=false)
However, I still need ProjectB to have that dependency for ProjectA. The error message seems to be m2eclipse looking for ProjectA in a remote repo. Project A should only be in .m2 directory, but I'm not sure how to tell m2eclipse to look there.
I think this may be just a m2eclipse specific issue because using mvn from the command line works fine and like I said previously, netbeans loads the projects and dependencies just fine.
I had this issue for dependencies that were created in other projects. Downloaded thirdparty dependencies showed up fine in the build path, but not a library that I had created.
SOLUTION: In the project that is not building correctly, right-click on the project and choose Properties, and then Maven. Uncheck the box labeled "Resolve dependencies from Workspace projects", hit Apply, and then OK. Right-click again on your project and do a Maven->Update Snapshots (or Update Dependencies) and your errors should go away when your project rebuilds (automatically if you have auto-build enabled).
It sounds like your m2eclipse install is using the embedded Maven, which has its own repository (located under user home) and settings.
If you open up the Maven preferences (Window->Preferences->Maven->Installations, you can add your Maven installation by selecting Add... then browsing to the M2_HOME directory.
(source: sonatype.com)
For more details see the m2eclipse book
For me maven was downloading the dependency but was unable to add it to the classpath. I saw my .classpath of the project,it didnt have any maven-related entry. When I added
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
the issue got resolved for me.
One of the reason I found was why it doesn't find a jar from repository might be because the .pom file for that particular jar might be missing or corrupt. Just correct it and try to load from local repository.
Okay I fixed this thing. Had to first convert the projects to Maven Projects, then remove them from the Eclipse workspace, and then re-import them.
I had problems with using m2eclipse (i.e. it did not appear to be installed at all) but I develop a project using IAM - maven plugin for eclipse supported by Eclipse Foundation (or hosted or something like that).
I had sometimes problems as sometimes some strange error appeared for project (it couldn't move something) but simple command (run from eclipse as task or from console) + refresh (F5) solved all problems:
mvn clean
However please note that I created project in eclipse. However I modified pom.xml by hand.
This could be a problem if you are using a custom 'Settings.xml', with a different <localRepository> configured in it.
Eclipse will be using the default installation of MAVEN, and will be using the default location for the User to look for the local Maven repository, which on Linux systems would be '/home/${USER}/.m2/'
Eclipse can be easily configured to use the customized 'Settings.xml', by doing the following:
Goto -> Window -> Preferences -> Select 'Maven' -> Select 'User Settings'
Under 'User Settings', select the custom 'Settings.xml' file, for 'User Settings' by clicking 'Browse' and selecting the customized 'Settings.xml'.
Click on 'Update Settings', if the 'Local Repository' Textbox does not show the custom location from the file above, just key in the location and click 'Reindex'.
Click 'OK'
After this, you could proceed to select your project from the 'Project Explorer', right click, Select 'Maven' > 'Update Project'. Make sure that your project is selected (ticked) in the Window, and click 'OK'.
This should help to resolve the issue, if using custom 'Settings.xml' for Maven.
Hope it helps.