Spring Boot: passing system properties to maven - spring

I tried:
mvn -Dspring.profiles.active=dev spring-boot:run
but it does not affect my default configuration. I've googled a little and found:
mvn -DargLine="-Dspring.profiles.active=dev" spring-boot:run
but if fails as well.
When I run:
mvn package
and then:
java -Dspring.profiles.active=test -jar target/app-1.0.0.jar
it works as expected (the profile is changed) but fails to find file from resource dir (FileNotFound exception) which is loaded this way:
new File(getClass().getClassLoader().getResource("data.yml").getFile())
There is no problem with this file when maven is used to run the app.
Any suggestions?

With the current version (>= 2.0.0.RELEASE), the parameter is -Dspring-boot.run.jvmArguments
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
Source: Running your Application with Maven
To activate a speciffic profile, there is a shortcut available:
mvn spring-boot:run -Dspring-boot.run.profiles=dev
Source: Specify Active Profiles

Try running your application with:
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=dev"
I don't know which version your are using but check this issue too

I also tried many ways. But in the end, what worked for me was setting them in pom.xml as mentioned in the documentation.
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.2.RELEASE</version>
<configuration>
<environmentVariables>
<ENV1>5000</ENV1>
<ENV2>Some Text</ENV2>
<ENV3/>
<ENV4></ENV4>
</environmentVariables>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

Related

Athena latest JDBC driver jar AthenaJDBC42_2.0.14

https://docs.aws.amazon.com/athena/latest/ug/connect-with-jdbc.html
I've installed latest jdbc jar into my local mvn repo. While i'm trying to build my project I'm getting below error.
Failed to collect dependencies at Athena:AthenaJDBC42:jar:2.0.14.1000: Failed to read artifact descriptor for Athena:AthenaJDBC42:jar:2.0.14.1000: 1 problem was encountered while building the effective model for Athena:AthenaJDBC${env.JDBC_V}:${env.MAJOR_V}.${env.MINOR_V}.${env.REVISION_V}.${env.BUILD_V}
[ERROR] [ERROR] 'artifactId' with value 'AthenaJDBC${env.JDBC_V}' does not match a valid id pattern. #
Anyone have any idea, how to resolve this error.?
It is difficult to say with just the small Maven error snippet, but it looks like Maven doesn't recognize the property ${env.JDBC_V} or it contains some invalid value like a space.
I recommend to generate a Maven effective pom model (mvn help~effective-pom) and execute Maven in debug mode (mvn -X ...) to try to troubleshoot the cause.
I was facing the same error. It got resolved when I installed jar into repository using following command. (Ensure you deleted all existing instances of athena drivers jar files from .m2 before executing command)
mvn install:install-file -Dfile=/Users/chetanparekh/Downloads/AthenaJDBC42_2.0.14.jar -DgroupId=Athena -DartifactId=AthenaJDBC42 -Dversion=2.0.14.1000 -Dpackaging=jar
Snippet from my POM.
<build>
<plugins>
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<sharedLibraries>
<sharedLibrary>
<groupId>Athena</groupId>
<artifactId>AthenaJDBC42</artifactId>
</sharedLibrary>
</sharedLibraries>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>Athena</groupId>
<artifactId>AthenaJDBC42</artifactId>
<version>2.0.14.1000</version>
</dependency>
</dependencies>
Thank you for your answer.
I finally resolved it using
mvn install:install-file -Dfile=/Users/dk/Downloads/AthenaJDBC42_2.0.14.jar -DgroupId=Athena -DartifactId=AthenaJDBC42 -Dversion=2.0.14
Removed the build version(1000) and -Dpackaing=jar, it worked. I'm not sure what could be the problem with build version .1000 and -Dpacking=jar.
Thanks Again.!

Can maven exec:java be used with an arbitrary java class from the command iine

We have a few java/scala classes with main methods that would be useful to run via mvn exec:java.
Is it possible to do so without specifying the classes in the pom.xml? The examples that I have seen look like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions><execution>
<goals><goal>java</goal></goals>
</execution></executions>
<configuration>
<mainClass>some.main.MyClass</mainClass>
</configuration>
</plugin>
And then get executed as:
mvn exec:java -Dexec.mainClass="some.main.MyClass"
The intent is to be able to run
mvn exec:java -Dexec.mainClass="some.other.main.OtherClass"
even though it were not specified in the pom.xml.
When attempting to run that the error is
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project sentiment: An exception occured while executing the Java class. myapp.MyMain
In other words the class specified on the command line is ignored in favor of the one listed in the pom.xml.
The Documentation at https://www.mojohaus.org/exec-maven-plugin/usage.html
says:
If you want to execute Java programs in the same VM, you can either
use the command line version
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"]
This works without adding anything to your pom.
As written in the comment, this only works, if you don't have a configuration in your pom. If you do have a configuration in your pom, you can use a property in it, which can be overridden from the command line
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions><execution>
<goals><goal>java</goal></goals>
</execution></executions>
<configuration>
<mainClass>${my.mainClass}</mainClass>
</configuration>
</plugin>
and predefine the property (nested directly in <project>)
<properties>
<my.mainClass>some.main.MyClass</my.mainClass>
</properties>
then you can run with
mvn exec:java -Dmy.mainClass="aaa.Test"
Looking from your usage if you are using the -Dexec.mainClass everytime, i would suggest you can get rid of the mainClass in the plugin configuration. The benefit of defining the mainClass would be to run like : mvn exec:java & it picks the main class from your defined class in pom.xml.
Once you remove the mainClass from pom.xml, you should be able to use any mainClass in the maven exec plugin usage from command line.

Running multiple SpringBootApplication classes from a single maven project

Is there a way to specify which SpringBootApplication's main class to run when running mvn spring-boot:run? The docs say I can use mainClass parameter to specify which main class to run. But I am not sure how to specify it in command line. I have tried mvn -DmainClass=mypackage.myclass spring-boot:run but it didn't work.
I got it working by having a placeholder in the plugin configuration of spring-boot
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${mainclass}</mainClass>
</configuration>
</plugin>
and then running different classes
mvn -Dmainclass=mypackage.myclass spring-boot:run
Two answers to your question
You have to create a MANIFEST.MF file in src/main/resources/META-INF/MANIFEST.MF and give an attribute like this as given below
Main-Class= com.yourfilename
You can use maven jar plugin to define main-class configuration in your manifest file, please use this links below which will help you.
link 1
link 2

Flyway seems to not be overriding properties

I have my standard flyway config in my pom file and I am trying to override in through system properties, as mentioned here.
Here is my configuration in the pom file:
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<url>dbUrl</url>
<user>dbUser</user>
<password>dbPass</password>
<schemas>
<schema>core</schema>
<schema>public</schema>
</schemas>
</configuration>
</plugin>
And following is the command line that I'm running:
mvn clean compile flyway:migrate -Dflyway.url=anotherDbUrl -Dflyway.user=anotherDbUser -Dflyway.password=anotherDbPass
The documentation in the above link says System properties > Maven properties > Plugin configuration. Am I missing something?
Good catch. This seems to have broken along the way. Could you please file an issue? I'll fix this in time for 2.2.1.

maven cargo plugin with tomcat6

I'm trying to setup maven cargo plugin. I have the following requirements:
tomcat 6
custom server.xml
custom context.xml
log4j jar deployed to tomcat lib
install tomcat on the machine if it's not there already
tie to maven's install lifecycle phase to deploy a war and restart the container
make the deployed war be ROOT.war
I've followed the following: http://www.java-tutorial.ch/maven/maven-tomcat-deployment-using-cargo. This isn't the complete feature set I want, and even it doesn't work entirely. This is what I get:
Can't load log handler "4host-manager.org.apache.juli.FileHandler"
[INFO] [talledLocalContainer] java.lang.ClassNotFoundException: 4host-manager.org.apache.juli.FileHandler
And then when mvn install returns I do ps -ef and there's no tomcat process.
Also it copies the war to ROOT.war but the old ROOT/ directory is not replaced so the new ROOT.war doesn't actually get deployed.
For the "install tomcat if not already there" requirement, it seems like this should be absolutely straightforward, yet when I provide
<zipUrlInstaller>
<url>http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.zip</url>
<extractDir>/usr/local</extractDir>
</zipUrlInstaller>
and run mvn cargo:install, it throws this:
org.codehaus.cargo.container.ContainerException: Failed to get container installation home as the container has not yet been installed. Please call install() first.
Which is puzzling. It wants me to call install first, but I AM calling install.
Ideas?
Link you followed has given demo for cargo 1.0.6. Recent version available is 1.1.1 so I suggest you to use recent and there is certain changes in child tags
As described in post http://cargo.codehaus.org/Deploying+to+a+running+container. There are ceratin changes in child tags of ZipUrlInstaller.
<!--
Careful: As described in the ZipUrlInstaller documentation,
Cargo versions older than 1.1.0 accept only installDir, you therefore
need to set installDir instead of downloadDir and extractDir.
-->
Try to use maven archetype to create cargo sample project following post http://cargo.codehaus.org/Maven2+Archetypes. I suggest you to user "Single Webapp Module Archetype"
After setting up maven project, You can install tomcat 6 running mvn cargo:install -P tomcat6x command.
pom.xml snippet of "single webapp module archetype" which can be useful for you.
<profiles>
<profile>
<id>tomcat6x</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<wait>true</wait>
<container>
<containerId>tomcat6x</containerId>
<!-- download zip url -->
<zipUrlInstaller>
<url>http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.zip</url>
<downloadDir>${project.build.directory}/downloads</downloadDir>
<extractDir>${project.build.directory}/extracts</extractDir>
</zipUrlInstaller>
</container>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
where wait parameter true will give you option to check whether server is running or not.

Resources