Idea 14 - Maven 3 - pass parameters - java

I am looking for most efficient way to pass parameters to maven through idea 14 (I have just started working with idea).
When I want to compile and deploy my application through maven itself, I just run this command mvn clean package tomcat7:redeploy -P localhost -Daugage_env=local.
I dont know, how to pass this parameter -Daugage_env=local as default (or how to integrate it with localhost profile, which would be even better).
I did try maven-projects->myproject->lifecycle->right click on compile and create custom compile where I changed the Command line text to compile -Daugage_env=local, but it does not work.

You can define profile specific properties directly in pom.xml like so:
<profile>
<id>localhost</id>
<properties>
<augage_env>local</augage_env>
</properti‌​es>
</profile>
More information can be found in Maven documentation for build profiles.

Related

Passing parameters in docker-compose and into POM

I know there is a way to pass parameters in maven through the POM.
POM MAVEN
<properties>
<webproperty> ${webproperty} </webproperty>
</properties>
COMMAND LINE WITH MAVEN
mvn install "-Dwebproperty=chrome"
I recently switched over to Docker and I was wondering if there was still a way to pass the parameters through the POM? I was looking at some examples and was wondering if I was going about it the right way.
Docker YAML
build:
image: something/webtest
environment:
- HUB_HOST=hub
browser:
- BROWSER=${BROWSER_TYPE}
COMMAND LINE WITH DOCKER
docker-compose up BROWSER_TYPE=chrome
Also will this command still work.
System.getProperty("BROWSER_TYPE");
Thanks in advance!
The pom's parameters can not be passed in out side of pom xml. I think you can use a template file to generate each pom file seperately.
I figured it out:
Add an argument in the DockerFile.
ARG BROWSER
create the command in your jenkinsfile
BROWSER=chrome docker-compose up myrun
In the docker yaml file add it to the build
environment:
HUB_HOST=hub
browser= ${BROWSER}
Add it to the java command
-Dbrowser="$browser"
Use System.getProperty("browser") to call it in your code.

How to run maven commands automatically, not from command?

I am working on a project that I need to build a maven pipeline for versioning and release.
I am doing like this:
Change version manually by running mvn versions:set -DnewVersion=2.0.0.
Check the project detail and detect if it is snapshot or release. I have my own custom plugin and a parameter which returns the final version, e.g. 2.0.0-SNAPSHOT. BUT HOW CAN I return this value and feed it to another plugin?
I compile the project and generate the jar with new name, from 2.
And...
I know I can handle every step by a CL command but what I want to know and confused me is that how to embed everything in pom.xml and just run mvn install and nothing else.
Each plugin needs some arguments that should be able to change that. I know I can use the <argument> tag under <configuration>, but some of the plugins do not have the argument tag.
How can I have a solid solution and config everything in my parent POM beforehand?
Also I do have my own plugin but I am not sure if it is possible to embed all things to that and I just run myplugin:Install.
For building a SNAPSHOT, you usually just run mvn clean verify on your project. If you need special plugins, you can configure them in the configuration section following the advice in the plugin description.
For building a release version, you should use a build server (like Jenkins). There, you can construct all the steps much easier than in Maven. If for some reason, you cannot do that, use the maven release plugin.

Shorter way for debugging Maven builds with eclipse

I have a Maven project in eclipse, which I run with a Run configuration. That configuration does compile and exec:exec with a script (called runner) defined in my pom.xml dependent on the OS (.bat in Windows, .sh in Linux). The runners do OS-dependent stuff and then start Java with my application. Which runner to use is specified with profiles like the following:
<profile>
<id>WINused</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<runnerForLaunch>${basedir}/src/runners/windowsRunner.bat</runnerToUse>
</properties>
</profile>
So, when I want to run it, I use Alt+Shift+X, M and select the Maven config. Later, I just use Ctrl+F11.
When I have to debug it, I have to do the following:
Edit the pom.xml to use another runner script that adds -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=y to the Java call.
Launch the run configuration.
Launch a debug configuration that connects to the debugger.
My question is, can I somehow shorten that process? I regularly forget to undo my changes to pom.xml and use the runner I currently do not need.
Can't Maven somehow detect if I run it with Run as or Debug as and adjust variables depending on that?
If the runner config in your POM supports command line arguments:
Create another profile containing:
<profile>
<id>debug</id>
<properties>
<debugArgument>-agentlib: ...</debugArgument>
</properties>
</profile>
Use the new property in:
<runnerForLaunch>${basedir}/src/runners/windowsRunner.bat ${debugArgument}</runnerToUse>
Add debug to Profiles: in your debug configuration.
Use %1 or $1 at the Java call in your scripts.
Or:
Declare and supply a property value of <debugArgument>debug</debugArgument>.
Evaluate %1 or $1 in your scripts and call Java with different arguments accordingly.
Or:
Add a property debugArgument with 1) debug or 2) -agentlib: ... to Parameter Name / Value in your debug configuration.
Use the property in:
<runnerForLaunch>${basedir}/src/runners/windowsRunner.bat ${debugArgument}</runnerToUse>
1) Evaluate %1 or $1 for debug and call Java with different arguments accordingly or 2) use them at the Java call in your scripts.
Usually, you don't need to add debug options because eclipse simply adds them by calling "mvnDebug" instead of "mvn" when debugging a maven project. I suggest you simply run the shell script before you run your Java app, and start the Java app using exec:java in order to have it run inside the maven process that is attached to the eclipse debugger.

Maven install not respecting command line arguments

I have a project which I am attempting to install with maven. The pom.xml has a few properties in it which are modified when the maven install command is run depending on whatever version of a library we are attempting to build with:
<properties>
<some-version>0</some-version>
</properties>
The zero here is a placeholder, as we'll always specify a legitimate version during our build process. The version is then referenced later in the pom.xml to specify a few dependencies:
<dependencies>
<dependency>
<groupId>com.mycompany.myproduct</groupId>
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
</dependencies
Building is done via make with the following commandline:
mvn -Dsome-version=1.6.2
Maven is able to correctly resolve the version and build as expected. However, the version being installed in my local maven repository (/home/user/.m2) doesn't have the correct version. The pom.xml that is installed does not have the updated version I set in the command line:
user#ubuntu:~/$ cat /home/user/.m2/repository/com/mycompany/myproduct/myproject/1.0.0/myproject-1.0.0.pom | grep some-version -C 1
<properties>
<some-version>0</some-version>
</properties>
--
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
user#ubuntu:~/$
This is preventing any other project which depends on myproject from being able to build, as maven will complain that it can't find version 0 of someOtherProject:
[ERROR] Failed to execute goal on project myproject:
Could not resolve dependencies for project mycompany.myproduct:myproject:jar:1.0.0:
The following artifacts could not be resolved: com.mycompany.myproduct:someOtherProject:jar:0,
Could not find artifact com.mycompany.myproduct:someOtherProject:jar:0 in central (https://mycompany.com/artifactory/repo/) -> [Help 1]
What do I need to do for maven to install with the updated version in the pom? Obviously a terrible hackish solution would be to use sed and modify the pom file directly, but it seems that Maven should be able to actually leverage the command line settings when installing the pom. Otherwise the ability to set arguments on the command line seems remarkably limited in effectiveness.
Better you may set your property in pom.xml in <properties> tag like this -
<properties>
<property>
<name>some-version</name>
<value>1.6.2</value>
</property>
</properties>
If you use this then you don't have to provide the property each time you issue a mvn command from terminal.
mvn -Dsome-version=1.6.2 works as a substitution value for the scope of building than replacing the original POM with the new values. Hence is the behavior you see. I am not aware of any maven support to do so.
Under #JoopEggen's advice, I looked deeper into the maven versions plugin. It offered an update-property target which will actually update the pom.xml value on disk, rather than just passing in an overwrite during the build phase. I was able to solve my issue by calling
mvn versions:update-property -Dproperty=some-version -DnewVersion=1.6.2 -DsearchReactor=false -DallowSnapshots=true
in the makefile before calling mvn install. Disabling the reactor was necessary to prevent the plugin from rejecting values it couldn't find in the remote repo (see here), and allowSnapshots allows me to use version numbers such as 1.6.2-SNAPSHOT, useful when testing.

Pass properties to maven profile while running tests in intellij

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

Categories

Resources