Java 有没有类似 VirtualEnv 的东西?
对于 Java 或 JVM 语言,是否有类似于 Python virtualenv 的东西?
Is there anything similar to Python virtualenv for Java or JVM Languages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
据我了解,virtualenv 使您能够拥有单独的库安装路径,有效地分离“虚拟”Python 安装。
Java 没有“系统范围安装”库的概念(*):它总是在类路径中搜索要加载的库。由于可以(并且需要!)为每个应用程序定义类路径,因此每个应用程序都可以选择要加载的库和版本。
如果您再深入一层,并且有一个应用程序以某种方式同时需要同一库的两个不同版本,那么您甚至可以通过一些类路径技巧来做到这一点。它可能会变得复杂,但这绝对是可能的(OSGi 是支持这一点的一个示例,甚至具有两个独立 Web 应用程序的 Tomcat 也能做到这一点)。
我在 virtualenv 描述中看到了一些对安全性的引用:Java 内置了一个相当彻底的安全系统。在服务器应用程序中,它经常被关闭,因为这种方式更容易配置,但您可以轻松配置 Java 应用程序到底是什么允许做。
(*) 几乎有扩展或扩展库,但它们的使用并不多,甚至可以轻松地从任意目录加载。
From what I understand, virtualenv enables you to have separate library installation paths, effectively separate "virtual" Python installations.
Java doesn't have the concept of a "system-wide installed" library(*): It always searches the classpath for the libraries to be loaded. Since the classpath can be (and needs to be!) defined for each application, each application can pick-and-choose which libraries and which versions it wants to load.
If you go down one level deeper and have a single application that somehow needs two different versions of the same library at the same time, then you can do even that with some classpath trickery. It can get complicated, but it's definitely possible (OSGi is one example where this is supported, even Tomcat with two separate webapplications does this).
I've seens some references to security in the virtualenv description: Java has a pretty thorough security system built in. In server applications it's often turned off because it's just easier to configure this way, but you can easily configure what exactly a Java application is allowed to do.
(*) Almost, there are extensions or extension libraries, but they aren't used a lot and even those can easily be loaded from arbitrary directories.
Ant、Maven 和 gradle 等构建工具与
pip
或easy_install
最接近。virtualenv的概念是通过类路径完成的。所以Java并不真正需要virtualenv
Build tools like Ant, Maven, and gradle are the the closest thing to
pip
oreasy_install
.The concept of virtualenv is done by the classpath. So there is no real need of virtualenv for Java
是的(参见 http://www.jenv.be/),就像许多其他语言(Ruby、Python、 Go、R、Php 等等)。
Yes(see http://www.jenv.be/), like many other languages (Ruby, Python, Go, R, Php, etc. etc.).
我知道这可能有点晚了,但是 Groovy/Java 有 gvm http://gvmtool.net/ 这是Ruby renv 的 Groovy 版本。
我恭敬地同意高塔姆·K,卢瑟。项目的依赖关系和包版本管理与维护不同项目的隔离的自包含虚拟环境不同。
我的2分钱
-W
I know this may be a little late , but Groovy/Java has gvm http://gvmtool.net/ which is the Groovy version of Ruby's renv.
I would respectfully agree with Gautam K, luthur. Dependency and package version management for projects is not the same as an isolated self-contained virtual environment to maintain different project.
My 2 cents
-W
我也一直在寻找类似的解决方案来简化使用不同 Maven 版本/设置和/或 Java SDK 的项目之间的上下文切换,而不必每次都修改
M2_HOME
和JAVA_HOME
设置时间。为此,我开发了一个解决方案,可以根据每个项目设置(存储在
.mvn
中)使用适当的配置来帮助执行mvn
命令。文件夹)。请参阅: https://github.com/AlejandroRivera/maven-env
请注意,这只会有帮助如果您使用 Maven 来构建和/或运行您的项目。
I have also been looking for a similar solution to simplify switching context between projects that use different Maven versions/settings and/or Java SDKs without having to modify
M2_HOME
andJAVA_HOME
settings every time.To this end, I developed a solution that helps execute
mvn
commands with the appropriate configuration based on per-project settings (stored in a.mvn
folder).See: https://github.com/AlejandroRivera/maven-env
Be aware that this only helps if you're using Maven to build and/or run your project.
我对“Java 没有‘系统范围安装’库的概念”这一断言感到困惑。您会如何称呼 $JAVA_HOME/jre/lib 和 $JAVA_HOME/jre/lib/ext 中的 jar 文件?
不管Java是否“需要”像virtualenv这样的工具,似乎有一个可以让您在不同Java环境之间快速切换的工具(例如具有此类安全扩展的Java 6、Java 7等)会很方便- 即使它实际上在幕后所做的只是操纵 PATH、JAVA_HOME 和 CLASSPATH 环境变量。
I'm confused by the assertion that "Java doesn't have the concept of a 'system-wide installed' library". What would you call the jar files in $JAVA_HOME/jre/lib and $JAVA_HOME/jre/lib/ext?
Regardless of whether or not Java "needs" a tool like virtualenv, it seems that something that allowed you to quickly switch between different Java environments (e.g. Java 6 with such-and-such security extensions, Java 7, etc.) would be handy - even if all it was actually doing under the covers was manipulating the PATH, JAVA_HOME, and CLASSPATH env variables.
Maven,您可以显式指定在 java 项目中使用哪些包
Maven, you can explicitly specify which packages you would use in a java project
Java 作为一种语言不需要 virtualenv 的沙箱功能,但像 Jython 这样的 JVM 语言可以让 VirtualEnv 使用不同的环境而不会发生任何冲突。
这篇博文对此进行了概述
引用:
因此,在使用 Jython 时,可以使用不同的框架和包,而不会与全局包发生任何冲突。
Java as a language does not need the sandboxing features of virtualenv but a JVM Language like Jython can have VirtualEnv to use different environments without any conflict.
It is outlined in this blog post
Quote:
So when using Jython different frameworks and packages can be used without any conflict with global packages.