0

I have been working in XCode and with Objective-C in the last few years, but I knew that once I would have to transition to Eclipse or the like for Java projects. Case is, I do not particularly like Eclipse, which led me to a new development environment, namely IntellijIDEA. However, I am not at all familiar with how I should handle my files and how to add external libraries, nor do I know what my CLASSPATH is supposed to be. Where do I keep these external libraries in my project? I am also working with Maven and I am not quite sure why to use dependencies. If I include the .jar in my Classpath - or whereever I store my resources, classes etc. - why are there dependencies ? I have got one working project, but I am not comfortable releasing or using it while not exactly sure what I have done there. I understand how I can add external .jar files in IntelliJ, but where do I add these external libraries in my project ?

This is how I have done it up to now: Let's say I have downloaded a Maven project and I would like to use it in some other project. I use mvn install and then (in my Module Settings) I add the .jar from the target folder as an external library. Does IntelliJ keep a reference to that .jar file ? If that .jar is in my Downloads folder and I would like to transfer the project to a server, is this connection broken ?

4

2 回答 2

3

在 Maven 项目中,您将依赖的库添加到 pom.xml 文件中,无需将它们添加到 IDE 中的其他位置和/或通过 IDE 将它们链接到您的项目。只需向您的pom.xml文件添加一个新的依赖项。

maven 将所有工件(库)存储(如果没有另外设置)在一个文件夹中:~/.m2/,这是您保存依赖项的地方,也是您的本地 maven 存储库。

Maven 使您的生活更轻松,因为您不必再​​将 jar 添加到您的类路径中。确保您的 Maven 项目确实被您的 IDE 识别为 Maven 项目(通常它在您的项目视图中是一个小M )。

这是对 maven 的简短介绍,一开始对我有很大帮助:http ://maven.apache.org/guides/getting-started/maven-in-five-minutes.html ,尤其是为了了解生命周期.

正如您所提到mvn install的:您构建的工件将部署到您的本地 maven 存储库,您拥有的所有其他项目都可以使用此工件并将引用您部署的最新工件。

您可以像运行任何其他项目一样运行您的项目,单击运行按钮并选择您的主类。

如果您的项目生成一个可运行的 jar,您也可以在pom.xml文件中定义它。请参阅如何使用 Maven 创建具有依赖项的可执行 JAR?在一个 jar 中创建一个包含所有依赖项/库的 jar,或者如何使用 maven 创建一个可运行 jar,该 jar 在单独的“lib”文件夹(不是 uber jar)中包含依赖项,用于将 deps/libs 放入的可运行 jar一个单独的文件夹。无论如何,在这两种情况下MANIFEST,Maven 都会适当地生成文件(可执行 jar 所需的文件)。

于 2013-10-08T10:53:27.683 回答
1

尝试mvn idea:idea从 maven 的 pom.xml 为 IntellijIDEA 生成项目。然后只需打开项目并查看添加的所有库。

于 2013-10-08T10:45:45.233 回答