Is there a Java library similar to libconfig for C++, where the config file is stored in a JSON-like format that can be edited by humans, and later read from the program?
I don't want to use Spring or any of the larger frameworks. What I'm looking for is a small, fast, self-contained library. I looked at java.util.Properties, but it doesn't seem to support hierarchical/nested config data.
I think https://github.com/typesafehub/config is exactly what you are looking for. The format is called HOCON for Human-Optimized Config Object Notation and it a super-set of JSON.
Examples of HOCON:
HOCON that is also valid JSON:
{
"foo" : {
"bar" : 10,
"baz" : 12
}
}
HOCON also supports standard properties format, so the following is valid as well:
foo.bar=10
foo.baz=12
One of the features I find very useful is inheritance, this allows you to layer configurations. For instance a library would have a reference.conf, and the application using the library would have an application.conf. The settings in the application.conf will override the defaults in reference.conf.
Standard Behavior for loading configs:
The convenience method ConfigFactory.load() loads the following
(first-listed are higher priority):
system properties application.conf (all resources on classpath with
this name)
application.json (all resources on classpath with this
name)
application.properties (all resources on classpath with this
name)
reference.conf (all resources on classpath with this name)
I found this HOCON example:
my.organization {
project {
name = "DeathStar"
description = ${my.organization.project.name} "is a tool to take control over whole world. By world I mean couch, computer and fridge ;)"
}
team {
members = [
"Aneta"
"Kamil"
"Lukasz"
"Marcin"
]
}
}
my.organization.team.avgAge = 26
to read values:
val config = ConfigFactory.load()
config.getString("my.organization.project.name") // => DeathStar
config.getString("my.organization.project.description") // => DeathStar is a tool to take control over whole world. By world I mean couch, computer and fridge ;)
config.getInt("my.organization.team.avgAge") // => 26
config.getStringList("my.organization.team.members") // => [Aneta, Kamil, Lukasz, Marcin]
Reference: marcinkubala.wordpress.com
Apache Commons Configuration API and Constretto seem to be somewhat popular and support multiple formats (no JSON mentioned, though). I've personally never tried either, so YMMV.
There's a Java library to handle JSON files if that's what you're looking for:
http://www.json.org/java/index.html
Check out other tools on the main page:
http://json.org/
Related
I am building my own ROM and want to use a aar file Library in one system service.
Is there any way this could be achieved?
If you are using Android.bp this can be done via android_library_import module type (link to soong spec).
Also, if you try to grep the AOSP source tree, you will easily find many examples of its use, for example: packages/apps/Settings/Android.bp:
android_library_import {
name: "contextualcards",
aars: ["libs/contextualcards.aar"],
}
android_library {
name: "Settings-core",
....
srcs: ["src/**/*.java"],
static_libs: [
...
"contextualcards",
...
}
In my Skylark rule, I am looking through all my deps - some of them are maven_jar instances defined in my WORKSPACE file. For those, I would like to access the value of maven_jar.artifact, but as far as I can tell it isn't available. Is it possible to get at that value?
For example, if my WORKSPACE has:
maven_jar(
name = "com_google_guava_guava",
artifact = "com.google.guava:guava:20.0",
)
And my BUILD file has something like this:
my_rule(
name = "foo",
deps = ["#com_google_guava_guava//jar"]
)
In the implementation of my_rule, I would like to get the value com.google.guava:guava:20.0.
I think you'll need to file a feature request for this:
https://github.com/bazelbuild/bazel/issues/new
The instance of the maven_jar rule in the workspace file isn't available to the rules in BUILD files, only the rules which the workspace rule generates are (i.e., #com_google_guava_guava//jar). Off the top of my head, maven_jar would have to generate a rule into the jar's workspace which has an attribute with the value of artifact, and that rule would need to create a provider containing that value for other rules to consume.
(There does happen to be META-INF/maven/com.google.guava/guava/pom.xml inside the jar, which seems to have the information you want, but I don't know if you can rely on that for all jars from maven, but either way, the contents of the jar aren't available at analysis time (within the rule implementation))
I use wsdl2java to generate DTO Java classes. It adds current timestamp into the comments section of every file generated.
How to disable those timestamps?
Because I'd like to minify changes between two wsdl2java launches (the generated java sources are under RCS).
P.S. Java 7; wsdl2java comes from org.apache.cxf:cxf-codegen-plugin:2.6.16 although version 3 is also considered.
Use option -suppress-generated-date of underlying Apache CXF in wsdl2java configuration.
Fragment of a build.gradle file as an example:
wsdl2java {
...
wsdlsToGenerate = [
[
...
"-suppress-generated-date",
...
]
]
...
}
This option will change these comments in generated classes
/**
* This class was generated by Apache CXF 3.2.7
* 2018-11-23T10:12:12.986+02:00
* Generated source version: 3.2.7
*
*/
to these:
/**
* This class was generated by Apache CXF 3.2.7
* Generated source version: 3.2.7
*
*/
More details: http://cxf.apache.org/docs/wsdl-to-java.html
however, other with CXF 3.5.2 dates as
#Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava", date = "2022-09-24T16:22:10.990+02:00")
#Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v2.3.5", date = "2022-09-24T16:22:10+02:00")
still remain in code.
yes, the file heading comments are away but intention was not to have code cluttered with unwanted changes. the changes are tracked by git normally.
generated dates in code may help with very very old code, but generally they are not desirable. it would be even better to have one comment with date in Service than 20 very same comments spread around in code.
no one follows 20 dates spread around generated code. if no one reads that information, that information has no value and should be avoided.
The changes in WS contract are commonly followed in WSDL file, there is no need to have dates generated in code.
it might be partially useful, if the generated dates would track real changes, that means, it would ONLY update the date where the contents really changed. it is a bad idea to clutter all places with very same date.
In Gradle's Java projects we can use PMD via pmd plugin. To configure the rules which we want to use can do it in two ways:
ruleSetFiles - The custom rule set files to be used. See the official documentation for how to author a rule set file. Example: ruleSetFiles = files("config/pmd/myRuleSet.xml")
ruleSetsThe built-in rule sets to be used. See the official list of built-in rule sets.
With ruleSetFiles there is no problem you can find the names of the rules and to add or exclude ones, but in the documentation there is no information about the ruleStes? From where to find the exact names? From what I found from another projects the names are similar to the names from the PMD documentation but lower case. For example:
Braces - > java-braces
Clone - > java-clone
Implementation - >java-implementation
Code Size - > java-codesize
But this like Security Code Guidelines do not transform in -> java-securitycodeguidelines but just in java-sunsecure. I found that the names which works with PMD 5.1.1. are:
pmd {
ruleSets = [
'java-android',
'java-basic',
'java-braces',
'java-clone',
'java-codesize',
'java-comments',
'java-controversial',
'java-coupling',
'java-design',
'java-empty',
'java-finalizers',
'java-imports',
'java-j2ee',
'java-javabeans',
'java-junit',
'java-logging-jakarta-commons',
'java-logging-java',
'java-migrating',
'java-naming',
'java-optimizations',
'java-strictexception',
'java-strings',
'java-sunsecure',
'java-typeresolution',
'java-unnecessary',
'java-unusedcode'
]
toolVersion = '5.1.1'
ignoreFailures = true
}
How to find mapping between PMD names which are shown in their documentation and Gradle names?
The docs for RuleSetReferenceId are helpful, as is I believe this directory in the source tree. Basically put java- in front of any of these files to turn on the rules there.
In documentation code I see some things like this:
/*
* #(#)File.java 1.142 09/04/01
what does characters like #(#) meaning?
#(#) is the character string used by the Unix what command to filter strings from binaries to list the components that were used to build that binary. For instance what java on AIX yields:
java:
23 1.4 src/bos/usr/ccs/lib/libpthreads/init.c, libpth, bos520 8/19/99 12:20:14
61 1.14 src/bos/usr/ccs/lib/libc/__threads_init.c, libcthrd, bos520 7/11/00 12:04:14
src/tools/sov/java.c, tool, asdev, 20081128 1.83.1.36
src/misc/sov/copyrght.c, core, asdev, 20081128 1.8
while `strings java | grep '#(#)' yields:
#(#)23 1.4 src/bos/usr/ccs/lib/libpthreads/init.c, libpth, bos520 8/19/99 12:20:14
#(#)61 1.14 src/bos/usr/ccs/lib/libc/__threads_init.c, libcthrd, bos520 7/11/00 12:04:14
#(#)src/tools/sov/java.c, tool, asdev, 20081128 1.83.1.36
#(#)src/misc/sov/copyrght.c, core, asdev, 20081128 1.8
#(#) was chosen as marker because it would not occur elsewhere, source code controls systems typically add a line containing this marker and the description of the file version on synchronisation, expanding keywords with values reflecting the file contents.
For instance, the comment you list would be the result of expanding the SCCS keywords %Z% %M% %R%.%L% %E% where the %Z% translates into #(#).
From (hazy) memory, that was the tag used by SCCS back in the "good old days". Given that (to my knowledge), BitKeeper uses SCCS underneath, it could be BitKeeper.
It is usually something that is added automatically by the version control system.
That construct has no special meaning in Java. It is just some text in a comment.
It looks like something that's inserted by a version control system.