vscode fails to resolve dependencies declared in a maven profile - java

I have a problem with vscode and a maven project in which some dependencies are declared for a specific profile as follows:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
...
When I open this project in visual studio code, the compilation fail because the dependencies declared for this profile (e.g "dev") cannot be resolved.
Notes:
I have this problem only in vscode. When I lunch the compilation with maven command or in Eclipse IDE (specifying the maven profile to use), it's ok.
I have this problem only for the dependencies declared for a profile (for all other dependencies declared in the global "dependencies" section it's OK)
the "Java Extension Pack" and "Maven for java" extensions are installed, and the version of vscode is 1.36.1
for "Maven for java" extension, I tell it to always use the "dev" profile whit this settings: "maven.executable.options": "-Pdev"
How can I make vscode to load the dependencies of my maven "dev" profile?

Related

Spring Boot from Maven Profile in POM

I have a spring boot application that has dev and prod settings. Following multiple instructions found during searches I have an application.properties file which has:
#-----------this will load the prod or dev according to the #activatedProperties# value which is set from the pom when built
spring.profiles.active=#activatedProperties#
Then there are two files:
application-dev.properties
application.prod.properties
I then have a pom which has the spring-boot pom as a parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
I then set the profiles up as follows:
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
If I then execute a build as follows:
mvn -DskipTests -Pprod clean compile package
After doing this the application.properties file shows:
#-----------this will load the prod or dev according to the environment
spring.profiles.active=dev
Note that it did not use prod as requested but instead dev. In fact it simply execute the activeByDefault profile not matter what I do. Has anybody else seen this and have any ideas as to what is happening. As you can imagine it is really annoying to have the deploy instructions say 'edit the POM file to move the activeByDefault property from the dev to prod profile'!
Solved!
Eclipse is getting in the way; as the build happens eclipse is reinforcing the default profile; this happens in :
Version: 2019-09 R (4.13.0)
Build id: 20190917-1200
If you close eclipse and run from the command line it works just fine.
If you use eclipse then unchecking the Refresh on access under Preferences -> General -> Workspace then it works. The clue was from another stack overflow question : Eclipse + maven : profile ignored
Thanks to all who took the time to reply.

IntelliJ resolve dependencies of profile

I have a Java project with multiple modules. In this project, one module has a dependency to another one, which is only needed for a specific profile and hence is defined like that:
<profile>
<id>myProfile</id>
<dependencies>
<dependency>
<groupId>MyGroupId</groupId>
<!-- ... -->
</dependency>
</dependencies>
<!-- ... -->
</profile>
This works fine when building manually with maven like that:
mvn clean install -P myProfile
When using the IntelliJ build however, the dependency doesn't get resolved.
I've tried the option to delegate IDE build/run actions to maven, adding a property for maven in Build, Execution, Deployment > Build Tools > Maven > Runner (namely -P -> myProfile), and much more which is most likely not of interest.
Is it possible to configure IntelliJ to resolve the dependencies for a specific profile?
To help Intellij Idea to understand about your maven profile and maven object mode, you set as default profile in maven so that by default it will be recognized and run by any IDE. I provide below the code snippet.
<profile>
<id>firstProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
..... Other code goes
</profile>
So inside profile, use this <activeByDefault>true</activeByDefault>. It may solve the problem.

IntelliJ: activate Maven profile when running Junit tests

I have declared some properties that are specific to Maven profiles. A part of my pom.xml:
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<my.properties.file>foo.xml</my.properties.file>
</properties>
</profile>
<profile>
<id>ci</id>
<properties>
<my.properties.file>bar.xml</my.properties.file>
</properties>
</profile>
</profiles>
I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".
I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.
You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml).
Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.
Hope this can help you.
Just finally solved it.
<profile>
<id>profile-to-be-activated-on-build</id>
<activation>
<activeByDefault>false</activeByDefault><!-- on your flavor -->
<property>
<name>mvn-profile-env-var-trigger</name>
</property>
</activation>
</profile>
Goto JUnit default profile (aka configuration template). Add into JVM args:
-Dmvn-profile-env-var-trigger
You may need to manually reload maven profiles in IDE.
Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).

Maven : Missing artifact com.sun:tools:jar:1.6.0 compile time exception in POM.xml [duplicate]

This question already has answers here:
JDK tools.jar as maven dependency
(8 answers)
Closed 8 years ago.
I am getting one weird issue and getting a compile time exception in my pom.xml when i am trying to add dependancy for tools. jar displayed as below(Missing artifact com.sun:tools:jar:1.6.0)
I have set my JAVA_HOME variable as below:
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_34
When i hardcode it to the actual path of JDK1.6 i dont find any error as below.
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>C:\Program Files\Java\jdk1.6.0_34\lib\tools.jar</systemPath>
</dependency>
but i know its not good practise. Request guidance in resolving this error.
java.home is a System property which generally points to the jre directory and you are getting an error as you have pointed to a jar which doesn't exist.
In case you want to refer to an environment variable within your pom file, use the below syntax.
${env.variable_name}
In your case, it should be ${env.JAVA_HOME} as seen below
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
Update: As lexicore has mentioned, this wont work with MAC as the MAC JDK has a different file structure.
In Maven, ${java.home} points to the JRE directory used by the JDK, not JDK itself. Please see this question:
Java_home in Maven
So instead of
${java.home}/lib/tools.jar
which assumes the JDK directory you should have used
${java.home}/../lib/tools.jar
However, this is only a half of the solution. The problem is that under Mac, the directory structure is different. You have to user profiles in order to make your build relibaly work cross-platform.
Please see this question:
JDK tools.jar as maven dependency
And, specificaly, this answer (which IS the correct answer and not the one accepted by the OP there).
This is how Oracle handles it in one of their POMs:
<!-- JDK dependencies -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>${tools.jar}</systemPath>
</dependency>
And then in profiles:
<profile>
<id>default-tools.jar</id>
<activation>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<tools.jar>${java.home}/../lib/tools.jar</tools.jar>
</properties>
</profile>
<profile>
<id>default-tools.jar-mac</id>
<activation>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<properties>
<tools.jar>${java.home}/../Classes/classes.jar</tools.jar>
</properties>
</profile>
On Mac, the JDK has a different file structure. This is why you have to define these profiles.
See also the following posts:
JDK tools.jar as maven dependency
Build error: missing artifact com.sun:tools:jar:1.6
missing artifact sun.jdk:tools:jar:1.6.0:system
Missing artifact com.sun:tools:jar:1.5.0 maven

Maven Dependency error in Eclipse

I have a war artefact and I need use some of their classes from a jar.
I can't move the classes to another project, then I deploy the classes and resources included in my webapp as an "attached" artifact using the following configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
This will result in two artifacts being deployed: mywebapp-1.0-SNAPSHOT.war and mywebapp-1.0-SNAPSHOT-classes.jar.
To use those classes I Referencing the artifact as follows:
<dependency>
<groupId>mygroup</groupId>
<artifactId>mywebapp</artifactId>
<version>${project.version}</version>
<classifier>classes</classifier>
</dependency>
When I compiled from Jenkins everything works correctly, but when I run the tests locally from Eclipse can not find the reference classes. (java.lang.NoClassDefFoundError)
I think it might be a bug in the maven eclipse plugin, someone has any idea that can be happening?
Workaround is described on http://wiki.eclipse.org/M2E-WTP_FAQ:
A workaround exists though, we need to change the dependency whether the project is built in Eclipse or not. In your dependent project, you can configure the following :
<dependencies>
...
<dependency>
<groupId>com.company</groupId>
<artifactId>mywebapp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>${webClassifier}</classifier>
</dependency>
...
</dependencies>
...
<properties>
...
<webClassifier>classes</webClassifier>
</properties>
...
<profiles>
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<properties>
<webClassifier></webClassifier>
</properties>
</profile>
</profiles>
The m2e profile is automatically activated when the project is built with m2e, ignored in other circumstances. In that case only, the dependent project will use an empty classifier to reference the web project, which will be added to the classpath as expected.
My simple answer is the following link to the bug tracking system of Eclipse:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=365419
See the answers inside.
Yes it's a problem with Eclipse itself..
The solution within Eclipse just add the project manually within your workspace to the appropriate project where you need the classes out of your war project.

Categories

Resources