在Linux上设置Closure Compiler的好方法是什么?或者,Java .jar应该在Linux上的哪个位置?

时间:2021-08-14 23:31:28

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.

我想建立一个流程,我可以为我的webapps生成缩小和优化的JS,作为使用git将更新的代码推送到Web服务器的过程的一部分。

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

运行它非常简单:java -jar ./compiler.jar script.js

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

当然,如果我有十个项目,我不想要十个compiler.jars。

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

如果我坚持这里:/usr/local/lib/compiler.jar我的电话看起来很愚蠢:

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?

是否有一些目录,我可以粘贴我的罐子,以便我可以更容易地运行它们,而不是我必须为我想要使用的每个jar设置(符号链接)shell脚本(或者可能更好的shell命令别名)?

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?

例如,不应该有一个系统,我把我的jar放在一个全局指定的java lib目录中,之后我可以调用java closure script.js?

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

编辑:我尝试将它放在我在这里找到的jre / lib / ext中,但它没有用:

$ 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

4 个解决方案

#1


1  

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. :-)

我的回答是更多的建议,你不要重新发明*,这有希望会让你免于问题,你没有参与 - 但是,因为你已经在你自己的评论中建议将jar包括在内每个项目。 :-)

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.

我建议将jar放在每个项目中并使其自包含或更好:在每个项目中运行一个安装脚本,从远程位置获取jar。

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.

我们将项目结构转换为Grunt任务(http://gruntjs.com)Grunt包含一个闭包编译器插件(以及更多插件,例如sass)。每个项目都包含一个Grunt配置文件,该文件指定所有任务,例如test,build,您可以在给定项目上调用它。因此,您必须在计算机上安装一次Grunt,Grunt将为给定项目选择所有任务,并负责下载closure-compiler.jar并调用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.

所以,即使你现在想想:“我只需要封闭编译器”最终你可能需要另一个工具B并为工具B启动bash脚本。我建议你花更多的时间学习,例如Grunt,并使用社区的工作。

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.

旁注:在我们使用Grunt之前,我使用另一个taskrunner设置了build-toolchain,Gradle(http://www.gradle.org),这是Java项目的流行,其原因是closure-compiler.jar本身就是一个Java项目。这非常好用,但我终于注意到Grunt比Gradle有更多基于Web项目的支持。由于我相信正确的工作正确的工具,我的结论是:使用Grunt for Javascript-Projects,Gradle for Java / JVM。

#2


0  

If you using the java command directly, then you'll have to provide a path to the jar in question. It's probably easier to place the jar in one place and create a shell script that handles the invocation and jar path.

如果直接使用java命令,则必须提供相关jar的路径。将jar放在一个地方并创建一个处理调用和jar路径的shell脚本可能更容易。

#3


0  

I use an alias which I think is much more efficient than having a shell script sitting somewhere achieving roughly the same thing.

我使用一个别名,我认为这比使用shell脚本坐在某处实现大致相同的事情要高效得多。

alias closure='java -jar /opt/closure-compiler/compiler.jar'

In use:

closure --js i.js --js_output_file i.min.js

#4


0  

I would also put it in each and every project. This is to make them self-contained. Here is my code to do so:

我也会把它放在每个项目中。这是为了使它们自成一体。这是我的代码:

cd /path/to/your/project
sudo apt-get install -y default-jre
wget "https://dl.google.com/closure-compiler/compiler-latest.tar.gz"
tar -xf compiler-latest.tar.gz
rm compiler-latest.tar.gz README.md COPYING
mv closure-compiler* closure-compiler.jar # rename for easier use

#1


1  

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. :-)

我的回答是更多的建议,你不要重新发明*,这有希望会让你免于问题,你没有参与 - 但是,因为你已经在你自己的评论中建议将jar包括在内每个项目。 :-)

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.

我建议将jar放在每个项目中并使其自包含或更好:在每个项目中运行一个安装脚本,从远程位置获取jar。

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.

我们将项目结构转换为Grunt任务(http://gruntjs.com)Grunt包含一个闭包编译器插件(以及更多插件,例如sass)。每个项目都包含一个Grunt配置文件,该文件指定所有任务,例如test,build,您可以在给定项目上调用它。因此,您必须在计算机上安装一次Grunt,Grunt将为给定项目选择所有任务,并负责下载closure-compiler.jar并调用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.

所以,即使你现在想想:“我只需要封闭编译器”最终你可能需要另一个工具B并为工具B启动bash脚本。我建议你花更多的时间学习,例如Grunt,并使用社区的工作。

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.

旁注:在我们使用Grunt之前,我使用另一个taskrunner设置了build-toolchain,Gradle(http://www.gradle.org),这是Java项目的流行,其原因是closure-compiler.jar本身就是一个Java项目。这非常好用,但我终于注意到Grunt比Gradle有更多基于Web项目的支持。由于我相信正确的工作正确的工具,我的结论是:使用Grunt for Javascript-Projects,Gradle for Java / JVM。

#2


0  

If you using the java command directly, then you'll have to provide a path to the jar in question. It's probably easier to place the jar in one place and create a shell script that handles the invocation and jar path.

如果直接使用java命令,则必须提供相关jar的路径。将jar放在一个地方并创建一个处理调用和jar路径的shell脚本可能更容易。

#3


0  

I use an alias which I think is much more efficient than having a shell script sitting somewhere achieving roughly the same thing.

我使用一个别名,我认为这比使用shell脚本坐在某处实现大致相同的事情要高效得多。

alias closure='java -jar /opt/closure-compiler/compiler.jar'

In use:

closure --js i.js --js_output_file i.min.js

#4


0  

I would also put it in each and every project. This is to make them self-contained. Here is my code to do so:

我也会把它放在每个项目中。这是为了使它们自成一体。这是我的代码:

cd /path/to/your/project
sudo apt-get install -y default-jre
wget "https://dl.google.com/closure-compiler/compiler-latest.tar.gz"
tar -xf compiler-latest.tar.gz
rm compiler-latest.tar.gz README.md COPYING
mv closure-compiler* closure-compiler.jar # rename for easier use