Configuring Jetty 9 + Spring 4 add PropertySource file - spring

How to add External PropertySource file to jetty 9 jetty.xml ?
i use spring annotation and External PropertySource file
#PropertySources({
#PropertySource(name = "arm", value = "${propertySource}")
})
public class SecurityConfig extends WebSecurityConfigurerAdapter {
when I run the application through maven I use paramentr like propertySource:
mvn -DpropertySource=file:/etc/jetty/arm.properties jetty:stop jetty:run
Its work perfect jetty start with /etc/jetty/arm.properties config params.
How to add -DpropertySource=/etc/jetty/arm.properties like a parametn to start jetty ? hoe to configurable jetty.xml ?
I read docs
http://www.eclipse.org/jetty/documentation/current/jetty-xml-usage.html
and add string to jetty.xml :
<SystemProperty name="propertySource" default="file:/etc/jetty/arm.properties"/>
but this does not work, and jetty fail.

The use of <SystemProperty> is to evaluate a named system property and use it in an XML file as a value for whatever thing you are attempting to configure.
There's 2 ways you can accomplish this using the jetty-distribution.
1: Simply use the Java JVM command line to add the property
$ cd /path/to/mybase
$ java -DpropertySource=file:/etc/jetty/arm.properties -jar /path/to/jetty-dist/start.jar
2: Allow the ${jetty.base} to manage the Java JVM properties
In your ${jetty.base} add the following 2 lines to your start.ini
--exec
-DpropertySource=file:/etc/jetty/arm.properties
Then you can just run Jetty normally ...
$ cd /path/to/mybase
$ java -jar /path/to/jetty-dist/start.jar
Bonus: Alternate technique
Since this is Spring, you could probably switch to using a classloader resource instead.
Run this command to enable resources classpath
$ cd /path/to/mybase
$ java -jar /path/to/jetty-dist/start.jar --add-to-start=resources
Then put your properties file in the new ${jetty.base}/resources
Lastly, reference your PropertySources via the Spring classloader resource reference instead.

In your ${jetty.base} add the following 2 lines to your start.ini
--exec
-DpropertySource=file:/etc/jetty/arm.properties
#service jetty9 start
[FAIL] Starting Jetty 9 Servlet Engine: jetty9 failed!
# sudo service jetty9 check
[ ok ] Checking arguments for Jetty:.
. ok
[ ok ] PIDFILE = /var/run/jetty9.pid.
[....] JAVA_OPTIONS = -Xmx256m -Djava.awt.headless=true -Djava.io.tmpdir=/var/cache/jetty9/data -Djava.library.path=/usr/lib -Djetty.home=/usr/share/jetty9 -Djetty.logs=/var/log/jetty9 -Djetty.state=/var/lib/jetty[ ok ty.state.
[ ok ] JAVA = /usr/lib/jvm/java-8-openjdk-amd64/bin/java.
[ ok ] JETTY_USER = jetty.
[ ok ] ARGUMENTS =.
[ ok ] Jetty 9 Servlet Engine is running with pid 23749.
solution 2
add to start.ini line --add-to-start=resourcesc
# sudo service jetty9 start
[warn] Starting Jetty 9 Servlet Engine: jetty9[....] /var/run/jetty9.pid exists, but jetty was not running. Ignoring /var/run/jetty9.pid ... (warning).
failed!
Where can I see the log ?

Related

tomcat jvm arguments lost

I want to run tomcat with a javaagent.
setenv.sh:
export JAVA_HOME=/home/ggfan/1-install/java/jdk1.8.0_271
export CATALINA_OPTS="${CATALINA_OPTS} -javaagent:/home/ggfan/2-work/centuari-space/jdbc-leak-detector/target/jdbcld.jar=/home/ggfan/3-tmp/logs/jdbcld,INFO,org.apache.commons.dbcp.PoolingDataSource\$PoolGuardConnectionWrapper"
call startup.sh, seems tomcat picked it correctly:
[ggfan#localhost bin]$ ./startup.sh
Using CATALINA_BASE: /home/ggfan/1-install/apache-tomcat-8.5.64
Using CATALINA_HOME: /home/ggfan/1-install/apache-tomcat-8.5.64
Using CATALINA_TMPDIR: /home/ggfan/1-install/apache-tomcat-8.5.64/temp
Using JRE_HOME: /home/ggfan/1-install/java/jdk1.8.0_271
Using CLASSPATH: /home/ggfan/1-install/apache-tomcat-8.5.64/bin/bootstrap.jar:/home/ggfan/1-install/apache-tomcat-8.5.64/bin/tomcat-juli.jar
Using CATALINA_OPTS: -javaagent:/home/ggfan/2-work/centuari-space/jdbc-leak-detector/target/jdbcld.jar=/home/ggfan/3-tmp/logs/jdbcld,INFO,org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
Tomcat started.
however, from tomcat log:
24-Sep-2021 15:45:04.437 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数: -javaagent:/home/ggfan/2-work/centuari-space/jdbc-leak-detector/target/jdbcld.jar=/home/ggfan/3-tmp/logs/jdbcld,INFO,org.apache.commons.dbcp.PoolingDataSource
inner class name after '$' sign get lost and the real argument received by my javaagent is the incomplete one.
what is wrong?
I am running with tomcat Apache Tomcat/8.5.64 on fedora 31
In order to split CATALINA_OPTS into words an pass them as arguments to java, the catalina.sh script uses eval:
eval exec "\"$_RUNJAVA\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
...
this obviously performs variable expansion on $PoolGuardConnectionWrapper. To prevent this escape the dollar sign twice:
export CATALINA_OPTS="${CATALINA_OPTS} ...PoolingDataSource\\\$PoolGuardConnectionWrapper"

Task execution is not working after lunching the task in spring cloud data flow

I have created one Spring boot application with #EnablesTask annotation and try to print the arguments in log.
package com.custom.samplejob;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
#EnableTask
public class TaskConfiguration {
#Bean
public CommandLineRunner commandLineRunner() {
return args -> {
System.out.println(args);
};
}
}
After I have run that mvn clean install to have the jar in local maven repo.
com.custom:samplejob:0.0.1-SNAPSHOT
Using custom docker-compose to run spring cloud data flow locally on windows using the below parameters
set HOST_MOUNT_PATH=C:\Users\user\.m2 (Local maven repository mounting)
set DOCKER_MOUNT_PATH=/root/.m2/
set DATAFLOW_VERSION=2.7.1
set SKIPPER_VERSION=2.6.1
docker-compose up
Using the below commend to register the app
app register --type task --name custom-task-trail-1 --uri maven://com.custom:samplejob:0.0.1-SNAPSHOT
Created task using UI(below URL) and lunch the task. Task was successfully launched.
http://localhost:9393/dashboard/#/tasks-jobs/tasks
These are the logs I can see in the docker-compose up terminal,
dataflow-server | 2021-02-15 13:20:41.673 INFO 1 --- [nio-9393-exec-9] o.s.c.d.spi.local.LocalTaskLauncher : Preparing to run an application from com.custom:samplejob:jar:0.0.1-SNAPSHOT. This may take some time if the artifact must be downloaded from a remote host.
dataflow-server | 2021-02-15 13:20:41.693 INFO 1 --- [nio-9393-exec-9] o.s.c.d.spi.local.LocalTaskLauncher : Command to be executed: /usr/lib/jvm/jre-11.0.8/bin/java -jar /root/.m2/repository/com/custom/samplejob/0.0.1-SNAPSHOT/samplejob-0.0.1-SNAPSHOT.jar --name=dsdsds --spring.cloud.task.executionid=38
dataflow-server | 2021-02-15 13:20:41.702 INFO 1 --- [nio-9393-exec-9] o.s.c.d.spi.local.LocalTaskLauncher : launching task custom-task-trail-1-48794885-9a0a-4c46-a2a1-299bf91763ad
dataflow-server | Logs will be in /tmp/4921907601400/custom-task-trail-1-48794885-9a0a-4c46-a2a1-299bf91763ad
But in task execution list, it's doesn't show the status and start date and end date of that task executions,
can some one help me to resolve this or am I missing anything here in local installation or task spring boot implementation wise?
I have enabled kubernetes on docker desktop and installed spring data flow server top of that.
And I tried with docker uri to register app and generate docker image using the jib-maven-plugin.
Now its works the sample task application in my case.

GitLab CI CD runner not loading properties file for profile

When I run a command mvn clean test -Dspring.profiles.active=GITLAB-CI-TEST in the GitLab CI CD it not loading properties file application-gitlab-ci-test.properties. It is loading only application.properties.
As file application-gitlab-ci-test.properties contains the different value for spring.datasource.url the pipeline is failing in the remote runners with error
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
Of course, this error is expected as properties file application.properties refers to the localhost database.
Code which loading application-gitlab-ci-test.properties:
#Profile("GITLAB-CI-TEST")
#PropertySource("classpath:application-gitlab-ci-test.properties")
#Configuration
public class GitLabCiTestProfile {
}
When I try to run the same command locally it's working as expected and in logs, I see the following records:
2020-03-30 19:23:00.609 DEBUG 604 --- [ main]
o.s.b.c.c.ConfigFileApplicationListener : Loaded config file
'file:/G:/****/****/****/****/target/classes/application.properties'
(classpath:/application.properties)
2020-03-30 19:23:00.609 DEBUG 604 --- [ main]
o.s.b.c.c.ConfigFileApplicationListener : Loaded config file
'file:/G:/****/****/****/****/target/classes/application-GITLAB-CI-TEST.properties' (classpath:/application-GITLAB-CI-TEST.properties) for profile
GITLAB-CI-TEST
I noticed that remote runners missing the second line. This one which loading application-GITLAB-CI-TEST.properties.
I also tried mvn clean test --batch-mode -PGITLAB-CI-TEST and this one too failing in the remote host but in local run working as expected.
I found the workaround for this issue by using the command
mvn clean test --batch-mode -Dspring.datasource.url=jdbc:mysql://mysql-db:3306/*******?useSSL=false&allowPublicKeyRetrieval=true
Can you please help me to solve this issue as this workaround is not satisfying me?
I found the solution to this issue.
I changed the name of the profile from the upper case (GITLAB-CI-TEST) to lower case (gitlab-ci-test), to match the lower case of profile name in properties file - application-gitlab-ci-test.properties.
Now in the remote runner, I'm using the following command:
mvn clean test -Dspring.profiles.active=gitlab-ci-test
Spring doc - link

spring.profiles.include does not work from command line parameter

I made trivial Spring Boot application and packaged it as JAR.
When I call
java -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
I got
No active profile set, falling back to default profiles: default
When I call
echo spring.profiles.include=B > application.properties
java -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
I got
The following profiles are active: B
When I call
rm application.properties
java -Dspring.profiles.include=B -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
I got
No active profile set, falling back to default profiles: default
But I expect The following profiles are active: B
Can I include Spring profile from command line?
Version: 1.5.1.RELEASE
PS. spring.profiles.active works as expected. But I don't want replace active profiles.
I see what's happening now, and I think it's arguably something I missed when making this change.
Prior to that change, spring.profiles.include was only considered when it was used in a configuration file. Following that change, spring.profiles.include is now considered from any source (System properties, command line arguments, etc) as long as spring.profiles.active is also set.
You can see this by running your sample with both properties set:
java -Dspring.profiles.include=alpha -Dspring.profiles.active=bravo -jar target/boot-active-include-0.0.1-SNAPSHOT.jar
Both the alpha and bravo profiles are active:
2017-02-09 11:30:49.467 INFO 40409 --- [ main] my.Example : The following profiles are active: alpha,bravo
I've opened an issue so that we can straighten this out.

Spring: Why I cant check for spring boot version without an error?

I am follow the guide for setting up spring boot with the following link.
http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#getting-started-installing-the-cli
section 10.2.2
when I type $ spring --version
I receive the error below.
/cygdrive/c/Users/Jesse/Documents/.sdkman/candidates/springboot/current/bin/spring: line 83: [: C:\Program: binary operator expected
Error: Could not find or load main class org.springframework.boot.loader.JarLauncher
You need to set the SPRING_HOME variable.
After setting, SPRING_HOME was not resolving correctly for me even though it was set in windows as a user and a system variable, and was also visible when running export via Git Bash. I ended up replacing the last line in my spring.sh file, essentialy forcing the classpath for the java command:
"${JAVA_HOME}/bin/java" ${JAVA_OPTS} -cp "/drive_letter/dir/to/spring/spring-x.x.x.RELEASE/lib/spring-boot-cli-x.x.x.RELEASE.jar" org.springframework.boot.loader.JarLauncher "$#"

Resources