有没有类似 VirtualEnv for Java 的东西?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7300148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:27:57  来源:igfitidea点击:

Is there anything like VirtualEnv for Java?

javapythonvirtualenvjvm-languages

提问by Gautam

Is there anything similar to Python virtualenvfor Java or JVM Languages?

有没有类似于Java 或 JVM 语言的Python virtualenv 的东西?

采纳答案by Joachim Sauer

From what I understand, virtualenv enables you to have separate library installation paths, effectively separate "virtual" Python installations.

据我了解,virtualenv 使您能够拥有单独的库安装路径,有效地分离“虚拟”Python 安装。

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.

Java 没有“系统范围安装”库(*) 的概念:它总是在类路径中搜索要加载的库。由于可以(并且需要!)为每个应用程序定义类路径,因此每个应用程序都可以选择要加载的库和版本。

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

如果您更深入一层,并且有一个应用程序以某种方式同时需要同一个库的两个不同版本,那么您甚至可以使用一些类路径技巧来做到这一点。它可能会变得复杂,但绝对有可能(OSGi 是支持此功能的一个示例,甚至带有两个独立 Web 应用程序的 Tomcat 也能做到这一点)。

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.

我在 virtualenv 描述中看到了一些对安全性的引用:Java 有一个非常全面的内置安全系统。在服务器应用程序中,它经常被关闭,因为这样配置更容易,但您可以轻松配置 Java 应用程序究竟是什么允许做。

(*) Almost, there are extensions or extension libraries, but they aren't used a lot and even those can easily be loaded from arbitrary directories.

(*) 几乎,有扩展或扩展库,但它们使用不多,甚至可以轻松从任意目录加载。

回答by Joachim Sauer

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

像 Ant、Maven 和 gradle 这样的构建工具是最接近pip或 的东西easy_install

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

virtualenv 的概念是由类路径完成的。因此,Java 并不真正需要 virtualenv

回答by user1467024

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.

我知道这可能有点晚了,但是 Groovy/Java 有 gvm http://gvmtool.net/,它是 Ruby 的 renv 的 Groovy 版本。

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.

我会恭敬地同意 Gautam K,luthur。项目的依赖和包版本管理与维护不同项目的隔离独立虚拟环境不同。

My 2 cents -W

我的 2 美分 -W

回答by Alejandro

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_HOMEand JAVA_HOMEsettings every time.

我也一直在寻找类似的解决方案来简化使用不同 Maven 版本/设置和/或 Java SDK 的项目之间的切换上下文,而不必每次都修改M2_HOMEJAVA_HOME设置。

To this end, I developed a solution that helps execute mvncommands with the appropriate configuration based on per-projectsettings (stored in a .mvnfolder).

为此,我开发了一个解决方案,该解决方案可以mvn根据每个项目的设置(存储在一个.mvn文件夹中)使用适当的配置来帮助执行命令。

See: https://github.com/AlejandroRivera/maven-env

见:https: //github.com/AlejandroRivera/maven-env

Be aware that this only helps if you're using Maven to build and/or run your project.

请注意,这仅在您使用 Maven 构建和/或运行您的项目时有用。

回答by luthur

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

Maven,您可以明确指定您将在 java 项目中使用哪些包

回答by gilbertpilz

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?

我对“Java 没有‘系统范围安装’库的概念”的断言感到困惑。你会怎么称呼 $JAVA_HOME/jre/lib 和 $JAVA_HOME/jre/lib/ext 中的 jar 文件?

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.

不管 Java 是否“需要”像 virtualenv 这样的工具,似乎可以让您在不同的 Java 环境之间快速切换的东西(例如带有如此这般安全扩展的 Java 6、Java 7 等)会很方便- 即使它实际上在幕后所做的只是操作 PATH、JAVA_HOME 和 CLASSPATH 环境变量。

回答by Gautam

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.

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

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.

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

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

所以在使用 Jython 时,可以使用不同的框架和包,而不会与全局包发生任何冲突。

回答by Ben Hyde

Yes(see http://www.jenv.be/), like many other languages (Ruby, Python, Go, R, Php, etc. etc.).

是的(参见http://www.jenv.be/),就像许多其他语言(Ruby、Python、Go、R、Php 等)一样。