I have created one maven project(Desktop application). Now I would like to make the same project, but I want to change some of the dependency versions.
For example I have lib-1.0.jar and lib-2.0.jar. Now I would like to debug one of my projects with lib-1.0.jar and another project with lib-2.0.jar.
What is the best approach to achieve this? I dont want to edit my pom by hand everytime I debug the project.
Set a property for the version number, something like:
<properties>
<lib.version>1.0</lib.version>
</properties>
And use that in the dependency:
<dependency>
<groupId>lib.group</groupId>
<artifactId>lib</artifactId>
<version>${lib.version}</version>
</dependency>
Now you can simply override this in a profile:
<profile>
<id>bleeding-edge</id>
<properties>
<lib.version>2.0</lib.version>
</properties>
</profile>
And run with the profile to use the different version:
mvn -P bleeding-edge clean install
Note that this will probably confuse your IDE no end - with some IDE's you can set the profiles used.
Related
when i'm navigating to a maven dependency class, i get a .class file with this comment on top and no doc is shown :
// Failed to get sources. Instead, stub sources have been generated by the disassembler.
// Implementation of methods is unavailable.
I don't have the issue with Java classes, only maven dependencies.
I have enabled download sources in the maven settings.xml and in the java extension pack settings.
Anyone has any idea how to solve this please ?
i had the same problem and i solve it,try this:
set settings.json in vscode,
"java.eclipse.downloadSources": true,"java.maven.downloadSources": true
add follow settings in settings.xml of maven:
<settings>
<!-- ... other settings omitted ... -->
<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
</settings>
3.reopen vscode and it will download javadoc
Could you try to set the "java.jdt.ls.java.home" manually? Set it to the JDK you installed on your computer.
Some people others had run across this kind of problem. It looks like an issue with the language server's embedded JRE.
I had this issue previously, and I couldn't change the language server's embedded JRE due the version being <17. The fix was adding the following line into settings.json.
"maven.terminal.useJavaHome": true
I maintain a small Java component published in Maven Central. In order to successfully publish the artifact, pgp/gpg signature files are required for all the artifacts. Following the directions here: https://central.sonatype.org/pages/apache-maven.html#gpg-signed-components, I can add this plugin to my pom.xml like this no problem.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
...
and I attach it to the verify or install phase. Now, when I run: "mvn install" or "mvn deploy" the .asc files are generated automatically as desired. Note: you also have to have your pgp/gpg machinery installed and configured properly for this to work.
This works OK for me as the artifact maintainer, but if someone else wants to clone my source code, modify it, and then run mvn install so they can make a locally modified version of the component available to other projects of theirs, they have to have all this pgp/gpg machinery setup properly too, which can be a pain. And they likely don't care about having signature files.
My question is, is there a recommended pom setup so the component maintainer can generate the .asc signature files when needed for a deployment (e.g., to Maven Central), but normal usage of Maven commands don't require signature generation?
I imagine I could use a profile in my pom to handle this. I did figure out a solution, which is pretty simple. Rather than adding the maven-gpg-plugin to my pom, I figured out I can actually just do this:
mvn clean install org.apache.maven.plugins:maven-gpg-plugin:sign deploy
This cleans everything, creates and installs all the artifacts locally, signs all the generated artifacts, and then deploys all the generated artifacts, including the signature files to the deployment target.
This does exactly what I want in 1 line without a pom modification, which is pretty cool. But are there other 'better' ways? Either way, I figured posting this way to do this might be helpful to others.
maven profile
One way using maven-gpg-plugin conditional it is put configuration of maven-gpg-plugin in profile.
You can simplify your profile by only add one property for skip / don't skip maven-gpg-plugin
Your project can look like:
<project ...>
<properties>
<gpg.skip>true</gpg.skip><!-- by default skip gpg -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<configuration>
<!-- ... -->
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<properties>
<gpg.skip>false</gpg.skip>
</properties>
</profile>
</profiles>
</project>
another plugin
You can also consider use another plugin for making signatures, eg: https://www.simplify4u.org/sign-maven-plugin/ sign-maven-plugin by default skips execution if private key not exist on running system.
Another feature of sign-maven-plugin is that don't need external software like gpg to make signature.
gpp.skip property
In my poms I set the gpp.skip property to true:
<properties>
<gpg.skip>true</gpg.skip>
</properties>
Its evaluated by the maven gpg plugin in the way, that signing is skipped by default, e.g.:
mvn install
To enable signing, you can set gpp.skip to false within pom.xml. But better you do it on the command line, so you don't have to modify your pom all the time:
mvn install -Dgpg.skip=false
You can also skip setting the property within the pom and skip signing like this:
mvn install -Dgpg.skip=true
But this way you (and other people working with your project) have to add this all the time to avoid signing. I find it more convenient when signing is turned off by default, as in the first solution. And I guess that's what you want.
I would like to know if using IntelliJ, is possible to run all test in the visual environment choosing a specific Junit category.
At the moment if you execute:
mvn clean test
you execute Fast Tests, but how to use IntelliJ to choose Slow or Fast?
Fragment of pom.xml
<profiles>
<profile>
<id>SlowTest</id>
<properties>
<testcase.groups>YOUR.PROJECT.test.categories.Slow</testcase.groups>
</properties>
</profile>
<profile>
<id>FastTest</id>
<properties>
<testcase.groups>YOUR.PROJECT.test.categories.Fast</testcase.groups>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
Many thanks in Advance
Juan Antonio
Your profiles are focusing the test run on specific categories. The JUnit Run/Debug configuration in IntelliJ also allows you to focus a JUnit run on a specific category.
You can access this configuration window from Run > Edit Configurations
Here's a screenshot showing a saved confoguiraiton named SlowTests which runs all tests having the category: com.stackoverflow.surefire.SlowTests:
You can save any such configuration by clicking on the file icon in the top left hand corner of this window and then that configuration will be available in the Run menu and you can even associate a keyboard short cut with it.
More information in the docs.
If you created your project using the pom.xml, in the "Maven Projects"-View you can activate the profiles you want to be active. There (Lifecycle) you can start the goal you want to be executed for each module as well.
How to get this: View->Tool Windows->Maven Projects
I am running the spring boot app by passing the spring active profile like below:
spring-boot:run -Dspring.profiles.active=dev
But how do I pass the spring.profiles.active when creating the package using maven. Following is the maven command:
mvn clean install
In case someone have the same situation, you can achieve this with 2 steps with spring boot and maven:
First in spring properties or yaml file, add the spring.profiles.active with it's value as placeholder:
spring.profiles.active=#active.profile#
Second, pass the value with maven:
mvn clean install -Dactive.profile=dev
When the jar/war packaged, the value will be set to dev.
you can also leverage the use of maven profiles:
<profiles>
<profile>
<id>dev</id>
<properties>
<active.profile>dev</active.profile>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<active.profile>test</active.profile>
</properties>
</profile>
</profiles>
Then run:
mvn clean install -Pdev
Maven is a build-time tool, the only way to make it modify the eventual runtime behaviour of the built artifact is to use its (build-time) profiles. This must not be confused with Spring's runtime profiles which are parameters instructing the Spring container to bootstrap application in a specific way.
In other words, the spring.profiles.active parameter doesn't get "baked into" the war file by maven, you'll still need to pass it when starting the application up, be it via command-line parameters or configuration file or whatever mechanism your servlet container offers.
For package, you may replace install with package
mvn clean install -P dev
You can use environment variables.
export SPRING_PROFILES_ACTIVE=some,test,profiles
mvn spring-boot:run
I was looking at some open source projects and noticed they have a setup like:
/app1/pom.xml
/app1/app1-mod1/pom.xml
/app1/app1-mod2/pom.xml
/app1/app1-mod3/pom.xml
So there is a master pom, and then all the modules have pom.xml also.
So in my situation I have the following:
1. spring mvc application
2. spring mvc application
3. shared model/db layer
So I guess I should be following the multiple pom.xml setup?
If yes, how will I get #3 to build before #2 and #1.
Should I be dropping down to ant to perform the actual build?
Any tutorials that you guys know of to walk me through this process?
I'm coding in IntelliJ.
The Maven reactor will build the modules in an appropriate order, you can tell it what you think the best way is by sorting them how you want.
I have a similar project setup like this:
<artifactId>root</artifactId>
<packaging>pom</packaging>
<name>Root</name>
<modules>
<module>event</module>
<module>admin</module>
<module>public</module>
</modules>
This is then used as a parent by the other modules. The event library is used in both the admin and public webapp modules. In the webapp modules define something like this:
<dependencies>
<!-- Events -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>event</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
A mvn install from the root works beautifully.
Maven will calculated the dependencies by itself it you setup your POMs so that
module #1 and #2 both depend on #3 maven will build #3 first without any additional configuration required.
The layout you use (multi module) is also required for the release plugin.
See also A Multi-module Project