Search code examples
javalinuxunixlibrariesgoogle-closure-compiler

What's a good way to set up Closure Compiler on Linux? Or, where should Java .jar's live on a Linux?


I'd like to set up a process where I can generate minified and optimized JS for my webapps as part of the process of pushing updated code to the webserver with git.

It's quite straightforward to run it: java -jar ./compiler.jar script.js

Of course if I have ten projects I don't want ten compiler.jars.

If I stick it here: /usr/local/lib/compiler.jar my call just looks stupid:

java -jar /usr/local/lib/compiler.jar script.js

It would make more sense to dump it in ~ like I do with everything else that doesn't have a place to go. It just feels sloppy.

Is there some directory that I can stick my jars into so that I can run them more easily, rather than my having to set up (symlinks to) shell scripts (or possibly better, shell command aliases) for each jar I want to use?

For example, shouldn't there be a system where I put my jar in a global designated java lib directory, after which point I may call java closure script.js?

Edit: I tried putting it in jre/lib/ext which I found here, but it did not work:

$ find /usr/lib | grep jre/lib/ext                         
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.25.x86_64/jre/lib/ext
.... bunch of other jar files here
$ cp ~/compiler.jar /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.25.x86_64/jre/lib/ext
$ java -jar compiler.jar script.js
Error: Unable to access jarfile compiler.jar

Solution

  • My answer is more a suggestion that you do not reinvent the wheel, and that hopefully will save you from problems, you did not run in - yet, since you were already suggesting in your own comment that it might be good to include the jar in every project. :-)

    I would suggest putting the jar in every project and make it self contained or better: run a setup script in every project, which fetches the jar from a remote location.

    You could script the setup script by hand or go one step further and use a task runner, to create your own build-toolchain, which you can reuse on multiple projects.

    We transitioned our project structure to Grunt tasks (http://gruntjs.com) Grunt contains a closure-compiler plugin (and many more plugins, e.g. sass). Each project contains a Grunt config file, which specifies all tasks, e.g. test, build, which you can invoke on the given project. So you have to install Grunt once on your machine and Grunt will pick up all your tasks for a given project and will take care of downloading the closure-compiler.jar and invoking the closure-compiler.

    So even, if you might think right now: "I only need the closure-compiler" eventually you might need another tool B and start bash-scripting for tool B. I suggest you invest more time in learning, for example Grunt, and use the work of the community.

    A side note: Before we used Grunt I set up the build-toolchain with another taskrunner, Gradle (http://www.gradle.org), which is popular for Java projects, motivated by the fact that the closure-compiler.jar is itself a Java project. This worked quite nice but I finally noticed that there is more Web-Project-Based support for Grunt than Gradle. As I believe in the right tool for the right job, my conclusion is: use Grunt for Javascript-Projects, Gradle for Java/JVM.