Pass properties to maven profile while running tests in intellij - java

I have a maven pom with profiles. In one of those profiles I refer to a system variable like this
<profile>
<id>kasper</id>
<properties>
<user>${username}</user>
</properties>
</profile>
When I invoke maven command line with the -Dusername=kasper all seems to be well.
The thing is, I import this project in IntelliJ. IntelliJ 13 allows to select profiles with which to run, through the Maven Tool Window.
When I select this specific profile to use while running tests, I can't seem to find how to replace this property correctly, i.e. to really tell IntelliJ that it has to pick this or that user name to run my maven tests, I tried a bit of everything and it doesn't seem to pick ot up.
Anybody an idea?
Kasper

In IntelliJ 14 maven profiles are not passed to tests (by default).
You can activate this mechanism via an option in Settings -> Maven -> Running Tests which let you enable passing of systemPropertyVariables to unit tests - this should cover your case.

View -> Tool Windows -> Maven Projects
then right clicking on needed phase, e.g. package it will be second item in context menu called
*Create [project_name] package ...*
And on some tabs you could override VM properties, add profiles, etc.

OK, I got it.
Go to the Intellij configuration on JUnit or TestNG (whichever one you are using). Under VMoptions... you can add the custom system property variables from Maven to debugger beside -ea option just like terminal command.
-ea -Dtest.environment=QA

Related

Derive mandatory SonarQube properties from pom file in Jenkins with SonarQube Jenkins Plung

I just use the SonarQube-Plugin for Jenkins and configured the Jenkins job to execute the SonarQube Scanner. If I'm leaving the analysis properties in the UI and the sonar-scanner.properties blank the mandatory properties are missing. According to this question at least the sonar.projectKey should be automatically derived from the maven properties <groupId>:<artifactId>. If I set the sonar.projectKey and the sonar.sources properties directly everything works fine. But how can I achieve that the the maven properties <groupId>:<artifactId> are used by the plugin?
Edit: If I set the Analysis properties in the UI to:
sonar.projectKey=${POM_GROUPID}:${POM_ARTIFACTID} and sonar.sources=. as described here
it works fine. But it has no real advantage because I have to insert the same information to every job. It would be better if I can set these properties in a central file like the sonar-scanner.properties because all project dependent information is set by maven properties.
My configuration of the Post-Build-Step
For a Maven project, you should better use the scanner for Maven. Enable "Prepare SonarQube environment" feature and simply use a standard Maven step to run mvn sonar:sonar.
Documentation: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzingwithSonarQubeScannerforMaven

How to delete dev profile in Intellij? and Why Maven profiles in Intellij has three states?

Question 1:
Erveytime I import a maven project from existing source, Intellij create a dev profile, but there is no such a dev profile in pom.xml. Here is my profiles in pom.xml:
But, there is a dev profile shown in maven projects window:
How to delete the inexplicable dev profile?
Question 2:
All the checkbox I have seen before have two states: checked or unchecked. However, the checkbox of a profile in Intellij has three states:
What's the difference?
To answer Your first question (from my comment):
the dev profile is defined in your settings.xml .
The second question:
If the checkbox is on, then when you run a maven task from within IntelliJ, the profile is explicitly added to the mvn call with -P dev.
If you switch it off (no checkmark), it is explicitly excluded in the mvn call: -P !dev.
And in the third state (grey checkmark) no special profile argument is passed and so wether the profile is active is decided how you defined it in your config.

How to force Eclipse to use maven for automatic build

I am using Eclipse ,Spring,tomcat server,maven.
I need to pass some parameters when i build the project with maven.Everything works fine when i do that using command line or when i right click on POM.xml and run it making run configuration changes in eclipse.
But as soon as i start my tomcat it is not able to find the value i want to pass in the parameter and it gives an exception
: Could not open ServletContext resource [/WEB-INF/classes/properties/application-${spring.profiles.active}.properties]
here ${spring.profiles.active} should be replaced the the argument value.
So eclipse is not using maven with argument to build and deploy on the server automatically.
Insert the required maven plugins (especially maven-eclipse-plugin) in the pom.xml for compiling the code and use the command mvn eclipse:eclipse in the project's base directory. This will do the required stuff to create a maven project in eclipse.
This is a very complicated subject where many things can go wrong. You need to be very explicit of how you are running tomcat. Are you using wtp? WTP would be the panel that says "Servers". Is this spring boot? Do have the m2eclipse wtp plugin installed (its very hidden and not easy to find)?
One easier way to get this right is just use springs version of eclipse.

How does one use a Maven profile?

I am new to maven and read bit about profiles at this and other resources
What i understood we use profile if we want to do override some default values or do some specific stuff.
In my legacy project i can see below profile where i just see overriding app.mode property. But i do not see using this property
further any where in build. I am sure what special being done here?
<profiles>
<profile>
<id>dev</id>
<properties>
<app.mode>dev</app.mode>
</properties>
</profile>
</profiles>
Maven profiles can be configured however you want them to be. Here are some use cases:
a) The project has several developers. For political and religious reasons, some of the developers are using Windows and some are using Linux. The team develops two profiles, one for Linux and one for Windows, setting project root paths, temporary folder paths, support program paths. These variables are injected into the dependency-calculation process, and subordinate processes that need this information. These profile settings are put into the settings.xml file for each programmer.
b) One of the developers is on loan to the project for a week. She doesn't want to reconfigure her computer for the project standard directory layout, etc., so she creates her own settings.xml that allows her to work in her own root directory.
c) The project uses a continuous integration server, such Hudson or Jenkins. The CI needs to have its own database for integration testing, so it would perhaps have modes like "devtest" and "prodtest" and "fulltest", depending on the requirements.
The profile would be activated if you pass -Pdev on the command line to Maven.
mvn -Pdev
See http://maven.apache.org/guides/introduction/introduction-to-profiles.html.
What this profile does is change the setting of the property app.mode from whatever it was before to dev. You will want to search your pom file (and the poms of any modules) for uses of $[app.mode} to see what's happening -- if anything.
Maven properties do not pass into Spring automatically, but it's not uncommon to find configuration in to re-export them as system properties to make them visible to Spring. This typically happens in the configuration for surefire and failsafe, but it's explicit, you'll still see ${app.mode} in a pom somewhere.
If the property being set there isn't used anywhere else in the build (did you check any sub-modules?) then the answer to your question is "In this case it does nothing."
However, in general, setting a property like that will make it available later in the build in case you needed it for something. If I had to guess, I'd say that this is the result of either an incomplete cleanup of code, or premature optimization.
I think your expectation is correct. Its used to pass on app.mode parameter to spring. See
How can I use maven profile Id value in spring bean files?
http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/

How to config TestNG test-output folder to be inside Maven target folder?

Folks!
I'm new on TestNG, and I'm trying it with Maven (Surefire plugin) and Eclipse.
When my tests are run by Maven its reports are put on target/surefire-reports as expected, but the same tests when run by Eclipse TestNG plugin I'd like them to be put inside a target subfolder also (and as so be cleaned by clean Maven goal and ignored by git, not saying about respecting Maven folder organization), but always go to the default ${basedir}/test-output.
There is a way I can do it? I'd prefer one that I do not need to manually config Eclipse settings (Couldn't TestNG in detect it's a Maven project and so change its output folder consistently?), but if there is no such way, anyone surely will help.
TIA and regards,
Heleno
There is a no way that testng plugin can automatically figure out.
You can set your testng preferences at the workspace or project level. If you set it at the workbench level then for all projects, by default, testng would put the results there.
Go to Window->Prefs->TestNG.
Change Output directory to be same as maven output directory.
You can follow the way that niharika_neo told...
Elsewise you can also create timestamp enabled folder that would be created for every maven run you do, so won't let your testng reports bother you much. If still bothering put testng reports to a specific folder.
You can provide a different folder in your pom as --
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
</properties>
and under configuration of your maven-surefire-plugin put --
<reportsDirectory>./test-output/${timestamp}</reportsDirectory>
You will need to reconfigure XML Reporter as described at http://testng.org/doc/documentation-main.html#logging-xml-reports to set the outputDirectory property.
From that page:
In order to configure this reporter you can use the -reporter option
in the command line or the Ant task with the nested
element. For each of these you must specify the class
org.testng.reporters.XMLReporter. Please note that you cannot
configure the built-in reporter because this one will only use default
settings. If you need just the XML report with custom settings you
will have to add it manually with one of the two methods and disable
the default listeners.
It isn't clear how this translates into a configuration for the Maven Surefire plugin.
I had the same issue and I resolved it by going to Window-> Preferences -> TestNG
From there I check marked Absolute output path and entered the absolute path in the Output directory. For example C:\TestResults

Categories

Resources