Can I run maven artifact from docker

Viewed 522

My goal is to setup a Dockerfile with maven:latest in order to be able to run my javascript code with the latest build of org.mozilla.rhino

FROM maven:latest
RUN [ "mvn", "dependency:get", "-Dartifact=org.mozilla:rhino:LATEST:jar" ]
RUN [ "mvn", "exec:java" "-Dexec.mainClass='org.mozilla.javascript.tools.shell.Main'" "-Dexec.args='src/index.js'"]

do I need a pom.xml in order to do that and if I do what should my pom.xml contain since my project only has javascript files?

PS: I have no previous experience with maven

1 Answers

Well, as you had hoped, you don't really need a pom.xml for your project. I don't know if you need this to be fully portable, but here's something I mocked up based on maven:latest. This is done by taking advantage of the fact that the rhino jar file which is downloaded contains a MANIFEST.MF file that tells a java command how to execute it.

contents of rhino-1.7.10.jar:/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Main-Class: org.mozilla.javascript.tools.shell.Main
Implementation-Version: 1.7.10
Implementation-Title: Mozilla Rhino
Implementation-Vendor: Mozilla Foundation
Implementation-URL: http://www.mozilla.org/rhino
Built-Date: 2018-04-09
Built-Time: 20:03:34

So:

index.js

print("Hello world");

command line

$ mvn dependency:get -Dartifact=org.mozilla:rhino:LATEST:jar # as you have currently
... # maven output snipped
$ find .m2 -name rhino*.jar -exec java -jar {} index.js \;
Hello world!

Dockerfile (untested)

FROM maven:latest
# Missing here is you copying your javascript into the image
RUN [ "mvn", "dependency:get", "-Dartifact=org.mozilla:rhino:LATEST:jar" ]
RUN [ "find", "/root/.m2", "-name", "rhino*.jar", "-exec", "java", "-jar", "{}", "src/index.js", "\;" ]

Edit:

I should also note that the .m2 user subdirectory contains a repository that houses all the artifacts which maven downloads. The maven:latest Dockerfile appears to set this up under /root/.

Related