Java 有没有类似 VirtualEnv 的东西?

发布于 2024-12-02 15:44:16 字数 142 浏览 1 评论 0原文

对于 Java 或 JVM 语言,是否有类似于 Python virtualenv 的东西?

Is there anything similar to Python virtualenv for Java or JVM Languages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

爱你不解释 2024-12-09 15:44:16

据我了解,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.

老子叫无熙 2024-12-09 15:44:16

Ant、Maven 和 gradle 等构建工具与 pipeasy_install 最接近。

virtualenv的概念是通过类路径完成的。所以Java并不真正需要virtualenv

Build tools like Ant, Maven, and gradle are the the closest thing to pip or easy_install.

The concept of virtualenv is done by the classpath. So there is no real need of virtualenv for Java

久光 2024-12-09 15:44:16

是的(参见 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.).

鸠书 2024-12-09 15:44:16

我知道这可能有点晚了,但是 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

只想待在家 2024-12-09 15:44:16

我也一直在寻找类似的解决方案来简化使用不同 Maven 版本/设置和/或 Java SDK 的项目之间的上下文切换,而不必每次都修改 M2_HOMEJAVA_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 and JAVA_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.

不羁少年 2024-12-09 15:44:16

我对“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.

那支青花 2024-12-09 15:44:16

Maven,您可以显式指定在 java 项目中使用哪些包

Maven, you can explicitly specify which packages you would use in a java project

我的奇迹 2024-12-09 15:44:16

Java 作为一种语言不需要 virtualenv 的沙箱功能,但像 Jython 这样的 JVM 语言可以让 VirtualEnv 使用不同的环境而不会发生任何冲突。

这篇博文对此进行了概述

引用:

为 Jython 安装 virtualenv。只需输入“jeasy_install
virtualenv”。一旦完成,你应该有一个“virtualenv”工具
Jython 安装的 bin 文件夹。

因此,在使用 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:

Get virtualenv installed for Jython. Just type "jeasy_install
virtualenv". Once that finishes you should have a 'virtualenv' tool in
the Jython installation's bin folder.

So when using Jython different frameworks and packages can be used without any conflict with global packages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文