Dropwizard - running a standalone command without adding bundles - dropwizard

I have a Dropwizard app with swagger-jaxrs2 generating a openapi spec. I would like to have a command run this simply as such java -jar app.jar generate build/spec.json for example, but because I have Hibernate configured, it requires me to specify the configuration file even though it is irrelevant to the command completely, and NPEs.
Trying to specify the configuration leads to this error:
unrecognized arguments: 'src/test/resources/configs/configuration.test.yml'
Because I haven't explicitly added the argument for the configuration.
Is there no way around this? I have to specify the config every time? Just really annoying :D
Edit: ended up just hijacking main and outputting it that way since I only need access to the classpath in this instance. Idea courtesy of Dropwizard command line input

You could define a special command extending io.dropwizard.cli.Command, which doesn't require passing configuration as paramater, since it is not a io.dropwizard.cli.ConfiguredCommand.
The just register it in your override of io.dropwizard.Application#initialize and call bootstrap.addCommand(new YourSpecialCommand());
It would allow you to call:
java -jar app.jar key-of-your-special-command <any args you define in your command>

Related

Why isn't telegraf reading environmental variables?

My goal is to put my telegraf config into source control. To do so, I have a repo in my user's home directory with the appropriate config file which has already been tested and proven working.
I have added the path to the new config file in the "default" environment variables file:
/etc/default/telegraf
like this:
TELEGRAF_CONFIG_PATH="/home/ubuntu/some_repo/telegraf.conf"
... as well as other required variables such as passwords.
However, when I attempt to run
telegraf --test
It says No config file specified, and could not find one in $TELEGRAF_CONFIG_PATH etc.
Further, if I force it by
telegraf --test --config /home/ubuntu/some_repo/telegraf.conf
Then the process fails because it is missing the other required variables.
Questions:
What am I doing wrong?
Is there not also a way of specifying a config directory too (I would like to break my file down into separate input files)?
Perhaps as an alternative to all of this... is there not a way of specifying additional configuration files to be included from within the default /etc/telegraf/telegraf.conf file? (I've been unable to find any mention of this in documentation).
What am I doing wrong?
See what user:group owns /etc/default/telegraf. This file is better used when running telegraf as a service via systemd. Additionally, if you run env do you see the TELEGRAF_CONFIG_PATH variable? What about your other variables? If not, then you probably need to source the file first.
Is there not also a way of specifying a config directory too (I would like to break my file down into separate input files)?
Yes! Take a look at all the options of telegraf with telegraf --help and you will find:
--config-directory <directory> directory containing additional *.conf files
Perhaps as an alternative to all of this... is there not a way of specifying additional configuration files to be included from within the default /etc/telegraf/telegraf.conf file? (I've been unable to find any mention of this in documentation).
That is not the method I would suggest going down. Check out the config directory option above I mentioned.
Ok, after a LOT of trial and error, I figured everything out. For those facing similar issues, here is your shortcut to the answer:
Firstly, remember that when adding variables to the /etc/default/telegraf file, it must effectively be reloaded. So for example using ubuntu systemctl, that requires a restart.
You can verify that the variables have been loaded successfully using this:
$ sudo strings /proc/<pid>/environ
where <pid> is the "Main PID" from the telegraf status output
Secondly, when testing (eg telegraf --test) then (this is the part that is not necessarily intuitive and isn't documented) you will have to ALSO load the same environmental variables into the current user (eg: SET var=value) such that running
$ env
shows the same results as the previous command.
Hint: This is a good method for loading the current env file directly rather than doing it manually.

java -jar saxon9he.jar persons.xml persons_users.xslt -o:persons_transformed.txt

I am having an XSLT to convert my xml into html format. I didn't achive any Experience about Saxon before but I'll try again and again.
This is the problem I had :
C:>java -cp saxon9he.jar net.sf.saxon.Transform -t -s:samples\date\books,xml-csl:samples\styles\books,xsl -o:c:\temp.html
Error: Main class net.sf.saxon.Transform could not be found or loaded
I did everything step by step from the Saxon Website :
https://www.saxonica.com/html/documentation/about/gettingstarted/gettingstartedjava.html
and I saw MR.Michael Kay Videos a lot before but it isn't work any way.
Can perhaps any one help me please ?
The problems are with Java, not with Saxon, in case that helps you look in the right place for documentation.
The message "Main class net.sf.saxon.Transform could not be found or loaded" is Java telling you that it can't find Saxon.
The bit of the command that tells it where to look is this:
java -cp saxon9he.jar net.sf.saxon.Transform
Here "java" is telling the operating system to load the Java virtual machine (which has succeeded). The "-cp" option is telling Java what Jar files to search for the relevant classes, and the "net.sf.saxon.Transform" part is saying what the relevant class is.
The problem is probably that there is no file called saxon9he.jar in the current working directory. Unfortunately Java doesn't give you an explicit error message for this, it just ignores this part of the command. Probably the current working directory isn't what you think it is. If you do "ls" or "dir" immediately before the "java" command, it will tell you what files are in the current working directory, which should include the saxon9he.jar file. If the JAR file is in some other directory, you can supply an explicit path, e.g. -cp c:/mike/java/saxon9he.jar.

Grails: how to define configuration profile when launching grails war with java command?

I have a grails 3.0 application packaged as war file, and I would like to run it with java command line. Things looks simple from the reference guide: java -jar my_war_file_name. But I cannot find a way to pass in the profile (dev/test/prod), and also it looks like -D option cannot overwrite the configuration as well. Any idea?
Best Regards
Sorry, I found the problem. I should put -D in front of -jar. So this is working:
java -Dgrails.env=prod -Dserver.port=xxxx -jar my_war_file

Configuring log4j at runtime

I'm using org.apache.tools.ant.listener.Log4jListener to manage logging with my ant script. The ant script is highly configurable and designed to be run different ways with different parameters and therefore I need to be able to log to files specified at runtime. I have a log4j.properties which specifies a log file to be build.log, and despite my attempts to launch ant redefining properties defined in log4j.properties have been unsuccessful.
The build ignores them and continues to write to build.log. I haven't found much support regarding writing to custom files unless it's in Java with their Logger class.
Perhaps I'm thinking this through wrong. log4j.properties isn't treated in the same way as a property file in an ant script (hence overrideable from the command line)? Is there a way I can do this intelligently without writing a custom task or something?
You setup your log4j.properties file using a system property that you can define dynamically on the command line. The property below is "${logfile.name}". An example log4j configuration would be like this:
# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd#HH\:mm\:ss,SSS}\:%c - %m%n
The command line option to pass a property, when calling "ant", is "-Dlogfile.name={runtime path/filename of log file}". Replace {runtime path/filename of log file} with your file name. When ant is run this value is set as a system property. That system property is then substituted into the log4j.properties at runtime.
http://ant.apache.org/manual/running.html

Erlang: specifying a working directory for mnesia?

How do I specify a working directory for mnesia without resorting to passing the "dir" parameter on the command-line?
In other words, can I specify a "working directory" for mnesia just before calling `mnesia:start()' ?
application:set_env(mnesia, dir, Dir).
Besides the method call mentioned in other responses here you can also specify this in a system configuration file or .app file specified with the -config parameter. See http://erlang.org/doc/design_principles/applications.html#id2270704 for more information. This allows you keep the configuration seperate from the code and avoid a lot of command line flags.

Resources