Shorter way for debugging Maven builds with eclipse - java

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.

Related

Command line parameters specified in NetBeans 8.2 turns up when running .jar from command line

I have searched for a way to change this (unwanted) behavior.
I set command line arguments in Project->Properties->Run->Parameters and run my program in NetBeans IDE. Arguments gets passed to my program just fine, but they also turn up when I run the .jar from a .bat-file or .sh-script after it is compiled.
So my customer ends up having my test arguments activated if I forget to remove the settings from project settings before distributing my .jar.
I would have expected this info to be removed from the .jar. At least after compiling without debug information.
Is there a way to remove the arguments from my distribution without removing them from my project settings?
Anyone else having the same problem?
You could add a configuration (Project->Properties->Configurations->Add), select the created configuration under "Project->Properties->Run" and specify the arguments only for this configuration.
But this way is NetBeans specific. If you are using Maven you should use profiles. Add something like this into your pom.xml:
<project>
...
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!--your command line arguments-->
<exec.args>hello world</exec.args>
</properties>
</profile>
</profiles>
...
</project>
You still would need to remember to deactivate your development profile before shipping but like this you can separate the configuration between development and production system in a clean way. (Especially if later on there should be other configuration values which should differ, like logging level, different paths and so on).

Spring Boot Profile-specific Properties build using Maven Configuration

How to build the environment specific war file for a spring boot application using maven.I have created 3 profile configuration files placed in src/main/resource/ folder.
application.prod.properties
application.dev.properties
application.test.properties
I am able to run application by specifying required profile type in the VM argument tab with the value "-Dspring.profiles.active=dev" while executing the project as spring boot application.
Here while running as spring boot application i am able to the specify the required profile. In the same way when I need to use for MAVEN install with different profile. Is it there any way to specify profile as part of VM argument list in Run Configuration for Maven Install goal.
I have limitation as not to touch the existing java code.
I am using STS IDE, Spring boot 1.5.2.RELEASE version, Java 1.8 and oracle db.
In the same way also help me in how to configure profiles in Jenkins.
My profile configuration has two requirements.
Run the application in STS IDE as spring boot app with the VM args.
Used the below VM ARGS
-Dspring.profiles.active=dev
Blockquote
(Here I am getting below exception while starting SpringBootApp locally in Dev Environment).
APPLICATION FAILED TO START
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "dev" are currently active).
Blockquote
How to do the same thing using maven install by specifying profiles dynamically to generate war file.I am unable to find the solution.
In your main application.properties file, set spring.profiles.active to #myActiveProfile# (or any name you wish)
spring.profiles.active=#myActiveProfile#
Add your application.properties file as a filtered resource so that the placeholder myActiveProfile is replaced during build phase.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
…
</build>
Add a profiles section to your pom.xml
<profiles>
<profile>
<id>dev</id>
<properties>
<myActiveProfile>dev</myActiveProfile>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<myActiveProfile>prod</myActiveProfile>
</properties>
</profile>
</profiles>
Basically, you are able to specify maven profiles when executing a particular goal. E.g mvn install -P profileName. Within a profile, you can create a property which can be passed from the command line when running a goal like mvn install -DmyActiveProfile=foo
Hope this helps.
Helpful Links
How to set spring active profiles with maven profiles
https://maven.apache.org/guides/introduction/introduction-to-profiles.html
Here, first of all I would suggest to rename your properties files to application-prod.properties, application-dev.properties and application-test.properties.
Second, maven install goal is to compile and build your project.
If you also want to run your application, when you do install I suggest to use spring-boot-maven-plugin.
And you can use a maven command something like below
mvn clean install spring-boot:run
Some links below for you information
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html
https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

Activate profile when finishing release using jgitflow Maven plugin?

I have a Maven profile documentation that I want to get activated when running mvn jgitflow:release-finish. I know that I can do:
mvn jgitflow:release-finish -Pdocumentation
because the documentation of the plugin states:
automatically copies any profiles (-P) and user-properties (-D) passed on the command line to the forked maven process when building
But that means that you cannot forget to add this profile manually.
Objective: I would like to be able to configure Maven so that this profile becomes active automatically (Or that I somehow can activate my profile when the 'release' profile is active).
The jgitflow:release-finish goal actually uses a default option useReleaseProfile defining:
Whether to use the release profile that adds sources and javadocs to the released artifact, if appropriate. If set to true, the plugin sets the property performRelease to true, which activates the profile "release-profile", which is inherited from the super pom.
This option has default value to true, hence when executing this goal will by default set the performRelease property to true.
Note that the release profile mentioned above is defined by the super POM, which is actually used by this plugin but also by the maven-release-plugin via the similar useReleaseProfile option.
You can then activate your profile based on this option as well, as following:
<profiles>
<profile>
<id>documentation</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
...
</profile>
</profiles>
This means that you can still esplicitely activate it via the -P option and that it will be automatically activated by the goal as well.

Pass Spring profile in maven build

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

Idea 14 - Maven 3 - pass parameters

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.

Categories

Resources