How to configure compiler level (1.6) in maven settings.xml? - java

I am familiar with configuring maven-compiler-plugin to use java 1.6 in pom.xml and using parent pom file.
Is there a way to configure java compiler level to java 1.6 in the settings.xml level (so that all my maven projects will use java 1.6)?
In this thread default maven compiler setting somebody told there is a way to configure it in settings.xml.
Can someone please explain how to configure?
PS: Another way to specify Java compiler level:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
-Siva

Simple answer No. In the settings file you don't configure things like this, cause it doesn't make sense and would make your builds not reproducible. The information about configuring the compiler plugin in settings.xml is rubbish.

source/target level can be configured in settings.xml, like this:
<profiles>
<profile>
<id>jdk-1.6</id>
<activation>
// can be replaced with other conditions
<jdk>1.6</jdk>
</activation>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>

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

Use one pom.xml for different projects

I use maven to build my java based program. It is work fine. But now I meet an issue which request me to build a revision for other users which is based on the same source code with a little different(e.g. different software name, and different resource file).
Does anyone have any idea about how to do it?
You need to use profiles. It's a little complicated to explain in an answer like this, but essentially, you will create different profiles within your POM for the different builds you want to do. You will choose the profile at build time, using, e.g., a variable definition in the mvn command line, and within the profile, you change any of the variables or settings that you need to change. Lots more info is available here.
Depending on the type of project, the most "maven" way to do this would be to split up the project into a parent with multiple children. Your main project is put into one module and each of user specific configurations goes into a different module, which can then depend on the common code.
Each of the user specific modules can have their own resources and unique configuration, which would make producing different named configurations easier. It would also make any user specific coding tweaks easier.
You can create different profiles within your POM for the different builds you want to do.
Here are some of the example part of POM.xml
<!-- Define profiles here and make DEV as default profile -->
<profiles>
<!-- dev Profile -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- qa Profile -->
<profile>
<id>qa</id>
<properties>
<env>qa</env>
</properties>
</profile>
<!-- prod Profile -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>

Categories

Resources