Simple Java snippet test turned into madness
I was working on another project and wanted to see how easy it is to check if the domain exists (i.e. it's not just the server that is down now), but w/o doing all those WHOIS request to all the different registries.
I found http://www.xbill.org/dnsjava/dnsjava-current/examples.html and wanted to try it out. Let's run this Java snippet quickly. After all, Java development is my daily job. Right?
Wrong! To write a (proper) minimal Java program that takes a single command-line argument, I had to do the following:
- Use
maven-archetype-quickstart
. Sets up thepom.xml
properly and creates a class with amain
method. I do not mention a ton of fields that need to be filled. - Add maven dependency for the library I want to play with. The most pleasant step of the this process.
- Adjust the snippet to my needs. Nothing special here.
- Try automating the execution of the
main
method withexec-maven-plugin
. Partial success from the second try, but program output is surrounded with maven log statements. - Try using
maven-jar-plugin
for the same purpose. Fail because the jar does not have the necessary classes on the classpath. maven-shade-plugin
was used to make a proper jar file that can be executed. Still, you need the java idiosyncrasyjava -jar target/xxx.jar
. Worse, you can't put that into README because it will break when the version changes.- Finally, we add the
appassembler-maven-plugin
plugin to the mix to produce binary wrappers.
P.S. I also had to look up a .gitignore
file for Java, Maven, and Intellij.
Usage
$ mvn clean package appassembler:assemble
$ sh target/appassembler/bin/dns-test google.com
Host google.com has nameserver ns2.google.com.
Host google.com has nameserver ns1.google.com.
Host google.com has nameserver ns4.google.com.
Host google.com has nameserver ns3.google.com.
$ sh target/appassembler/bin/dns-test dwvwdv3r.ed
Host dwvwdv3r.ed has no nameservers
Or
mvn clean compile exec:java -Dexec.args="facebook.com"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dns-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ dns-test ---
[INFO] Deleting /Users/andrew/code/dns-test/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ dns-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/andrew/code/dns-test/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ dns-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/andrew/code/dns-test/target/classes
[INFO]
[INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ dns-test ---
Host facebook.com has nameserver a.ns.facebook.com.
Host facebook.com has nameserver b.ns.facebook.com.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.952 s
[INFO] Finished at: 2016-11-06T02:05:35+01:00
[INFO] Final Memory: 15M/212M
[INFO] ------------------------------------------------------------------------
With a bit of magic, we can do even better:
$ mvn -q clean compile exec:java -Dexec.args="facebook.com"
Host facebook.com has nameserver b.ns.facebook.com.
Host facebook.com has nameserver a.ns.facebook.com.