Java: Where is Autowired.Value looking for a setting? - java

I've inherited a large-ish Java project using org.springframework.beans.factory.annotation.Autowired, among other things.
I have one module that starts like this:
#Autowired
public Application(
FlattenByFeedRunInvoker flattenRunInvoker,
#Value("${jobId:}") String jobId,
#Value("${instanceId:}") String instanceId,
#Value("${feed}") String feed,
When I run it under debug, using this config:
{
"type": "java",
"name": "Debug (Launch)-Application<tbsm-reporting-etl-flatten>",
"request": "launch",
"mainClass": "com.tbsm.reporting.etl.flatten.Application",
"projectName": "tbsm-reporting-etl-flatten"
}
I get the message:
[ERROR] 2019-07-23 10:10:58.137 [THREAD ID=main]
[CLASS=(SpringApplication:771)] - Application startup failed
java.lang.IllegalArgumentException: Could not resolve placeholder
'feed' in value "${feed}"
I'm guessing that the framework is trying to find a value labelled "feed" somewhere. FWIW I do have a file called "default.properties" in the "resources" directory for the project and that file has the line
feed=myfeed
It looks like that file or value is not being found. I'm wondering if it is just in the wrong directory wrt the program i'm running:
..\src\main\java\com\tbsm\reporting\etl\flatten\Application.java
vs
..src\main\resources\default.properties
Or if I need to do something else to tell it where to look. e.g. does the vscode launch config need something else?

Related

Can't figure out how to use Prettier Plugin in .prettierrc

I'm using Prettier for a Java project. Prettier doesn't format Java by default, so instead I installed this plugin here: https://github.com/jhipster/prettier-java
Following the directions in the README works fine, however, I'd prefer not to type a terminal command to reformat all my java files every time I modify anything. Instead I'd like to format a file anytime I save it. This is currently in my settings.json:
"[java]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true
However, using Shift+Alt+F, the default command for formatting in VScode, results in the error "There is no formatter for "java" files installed". How do I configure my .prettierrc file to properly use the plugin to format java files?
This is my .prettierrc file right now:
{
"plugins": [
"prettier-plugin-java"
],
"pluginSearchDirs": [
"./node_modules"
],
"overrides": [{
"files": "**/*.java",
"options": {
plugins: [
"prettier-plugin-java"
]
}
}]
}
Used same settings in .prettierrc and settings.json file
except
"[java]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
and added default formatter for java files as follows
Open command Palette... Ctrl + Shift + P (mac command + shift + P) and
search Format Document With...
select Configure Default Formatter...
choose Language Support for Java(TM) by Red Hat
Working sample
Now if I add following in settings.json
"[java]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
got the same issue as of yours
So the solution will be to remove above settings from the settings.json file.
settings.json file
{
"files.eol": "\n",
"terminal.explorerKind": "external",
"terminal.integrated.shell.osx": "/bin/zsh",
"editor.minimap.enabled": true,
"workbench.colorTheme": "Visual Studio Dark",
"editor.fontSize": 14,
"editor.fontFamily": "source code pro, Menlo, Monaco, 'Courier New', monospace",
"terminal.external.osxExec": "iterm.app",
"window.zoomLevel": 1,
"go.useLanguageServer": true,
"editor.formatOnSave": true,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"breadcrumbs.enabled": false
}
P.S.
Make sure Java Extension Pack vscjava.vscode-java-pack is installed, used v0.12.1
Ref:
https://github.com/redhat-developer/vscode-java/issues/220
Beautify / Format Java code in Visual Studio Code
https://code.visualstudio.com/docs/java/java-linting
https://github.com/jhipster/prettier-java
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

Jenkins Java: Get user who started the build

Under http://[JENKINS_NAME]/job/[JOB_NAME]/[BUILD_NUMBER]/
I can see Started by user [USER_NAME].
I want to get that username from my java application.
Any help is much appreciated.
You can make a http call to get all these details. URL to get those details is:
http://<Jenkins URL>/job/<job name>/<build number>/api/json
After the rest call, you will be getting this json.
{
"_class": "hudson.model.FreeStyleBuild",
"actions": [
{
"_class": "hudson.model.CauseAction",
"causes": [
{
"_class": "hudson.model.Cause$UserIdCause",
"shortDescription": "Started by user XXXXXX",
"userId": "xxx#yyy.com",
"userName": "ZZZZZZZZ"
}
]
},
{},
{
"_class": "jenkins.metrics.impl.TimeInQueueAction"
},
{},
{}
],
...
}
So All you have do is parse this json and get the value under javavar['actions'][0]['causes'][0]['userName']. Definitely it will be like that only. I maynot be sure about the indexes. You just try and figure out. Hope this helps.
Mostly for every page in the jenkins instance, you will be having REST API link. Please click on it to see the rest api url and its output for that url.
You could get the build user from Jenkins environment (i.e as an env var). If you use Jenkins 2 pipeline, For example:
pipeline {
//rest of the pipeline
stages {
stage('Build Info') {
steps {
wrap([$class: 'BuildUser']) {
sh 'java -jar <your_java_app>.jar'
}
}
}
}
In your java app you should be able to get the environment variable using System.getenv("BUILD_USER") or else you could pass it as a JVM arg. Ex: sh 'java -jar -DbuildUser=$BUILD_USER <your_java_app>.jar' and get the buildUser system property in the application.
On older version of Jenkins, you may use Build User Vars Plugin or Env Inject plugin. As in the answers on this question. how to get the BUILD_USER in Jenkins when job triggered by timer

Spring Config Server - resolvePlaceholders + enviroment variables

I've got question about resolving environment variables in shared files of config server.
My current setup is pretty minimal :
src/main/resources/shared/application.yml :
application:
version: 0.0.1-early
test: ${JAVA_HOME}
src/main/resources/application.properties :
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/shared
Using gradle with :
spring-boot-gradle-plugin:2.0.0.RELEASE
spring-cloud-dependencies:Camden.SR7
And then of course compile 'org.springframework.cloud:spring-cloud-config-server' in deps
Problem :
GET http://localhost:8888/apptest/application gives me :
{
"name": "apptest",
"profiles": [
"application"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/shared/application.yml",
"source": {
"application.version": "0.0.1-early",
"application.test": "${JAVA_HOME}"
}
}
]
}
So env variable is not resolved. Same thing is with :
http://localhost:8888/apptest/application?resolvePlaceholders=true
http://localhost:8888/lab/apptest-application.properties?resolvePlaceholders=true
http://localhost:8888/lab/apptest-application.properties?resolvePlaceholders=false
http://localhost:8888/apptest-application.properties?resolvePlaceholders=true
I've looked at Spring cloud config server. Environment variables in properties but solution didn't help me + there where few new versions since then. So I'm opening new question.
Actually it's not a bug and everything is fine. I did not understood how Config server works.
http://localhost:8888/apptest/application - returns yet not resolved value of ${JAVA_HOME}
When we get ei. into container "C" that pings Config Service for configuration and do curl http://config:8888/apptest/application we get the same - unresolved ${JAVA_HOME}
But when we look into Spring application ei. in container "C" and try to inject #Value("${application.test}") somewhere, we get proper value or info that env variable was not set.
It means that environment variables are resolved on client side.
Thanks to that I've understood how NOT production ready env_variables approach is.
Well the changes happened here https://github.com/spring-cloud/spring-cloud-config/commit/f8fc4e19375d3b4c0c2562a71bc49ba288197100 that removes the support of replacing the environment variables.
You can always add a new controller and override the behaviour of the EnvironmentPropertySource#prepareEnvironment

Error: Could not find or load main class Orders

So I have a folder where some files are sitting. One PDF (the homework assignment), one .vscode folder with the Launch.json inside for debugging, and 3 .java files.
These files are: Orders.java, Truck.java, and Car.java
Truck and Car contain classes, Orders contains the main program:
public class Orders {
public static void main(String[] args) {
//actual program inside
}
}
I used VS Code's gear button in the debugger pane to create the launch.json, which gave me this for a launch option:
"configurations": [
{
"type": "java",
"name": "Debug (Launch)-Orders",
"request": "launch",
"mainClass": "Orders",
"args": ""
},
I set some breakpoints hoping to be off to the races. However when I hit the green play button to run the program, I get the error listed in the title of the post: Error: Could not find or load main class Orders
Is there something wrong with Launch.json or is there something else that could be causing this issue?
Also, if I run the program in the VS Code provided terminal:
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
d.
PS C:\Users\Profile Name\Documents\RIT\Fall 2017\ISTE 200\HW\HW3> java Orders
The program runs fine but hits no breakpoints.
I'm using the Redhat Java Support extension, if that matters.

Errror when packaging sencha touch for ios debug

I'm trying to package a sencha application for ios. But when I in the terminal do: "sencha package build package.json" I get the following
: [ERR] null
at com.sencha.command.environment.AppEnvironment.(AppEnvironment.java:48)
at com.sencha.command.environment.BuildEnvironment.load(BuildEnvironment.java:193)
at com.sencha.command.Sencha.loadBuildEnvironment(Sencha.java:374)
at com.sencha.command.Sencha.main(Sencha.java:127)
I probably screwed something up with the certficates and provisioning profiles, but, I'm not sure if this is some kind of environment error, ie sencha cmd not finding path of Java. The error seems to suggest this, but the cmd works with other commands. I can, for example, create a new app with "sencha app generate myapp ..myapp".
So, my question is really, is the error caused by some environment problem, and if so, what to do about it, or is his a problem related to some errror in my packager.json. Here's my packager.json file.
All help much appreciated!
{
"applicationName":"app",
"applicationId":"com.appname",
"bundleSeedId":"xxxxxxxx",
"versionString":"1.0",
"iconName":"icon.png",
"icon": {
"36":"resources/icons/Icon_Android36.png",
"48":"resources/icons/Icon_Android48.png",
"57":"resources/icons/Icon.png",
"72":"resources/icons/Icon~ipad.png",
"114":"resources/icons/Icon#2x.png",
"144":"resources/icons/Icon~ipad#2x.png"
},
"inputPath":"/Applications/XAMPP/xamppfiles/htdocs/app/",
"outputPath":"../build/",
"configuration":"Debug",
"platform":"iOS",
"deviceType":"Universal",
"certificatePath":"../cert/mycert.p12",
"certificateAlias":"iPhone Developer:",
"certificatePassword":"",
"provisionProfile":"../cert/name.mobileprovision",
"notificationConfiguration":"",
"orientations": [
"portrait",
"landscapeLeft",
"landscapeRight",
"portraitUpsideDown"
]
}

Categories