简体   繁体   English

如何在Eclipse中从Maven创建项目

[英]How to create a project from maven in eclipse

I've installed the m2e plugin in Eclipse. 我已经在Eclipse中安装了m2e插件。 I'm trying to download the source and dependencies of MG4J and use it in my project. 我正在尝试下载MG4J的源代码和依赖项,并在我的项目中使用它。 In the upper-right corner there's a Maven link. 在右上角有一个Maven链接。 However, I don't know what to do with it! 但是,我不知道该怎么办! I downloaded the source and opened a file and found the SCM location. 我下载了源代码并打开了一个文件,找到了SCM位置。 I entered it in the "Checkout Maven Projects from SCM" project type, but when I click finish, nothing happened. 我在“从SCM签出Maven项目”项目类型中输入了它,但是当我单击“完成”时,没有任何反应。 How can I achieve this? 我该如何实现?

Update: 更新:

I'm new to the world of Java. 我是Java的新手。 What I'm trying to do is this: I wanted to use MG4J in my project. 我想要做的是:我想在项目中使用MG4J。 I think I might have to change its code. 我想我可能不得不更改其代码。 First I downloaded the source and dependencies archives and extracted them, added .jar files to the build path and everything compiled. 首先,我下载了源和依赖档案,并解压缩了它们,将.jar文件添加到构建路径中,并编译了所有内容。 But it failed to run on some occasions. 但是它有时无法运行。 I contacted the MG4J people. 我联系了MG4J人员。 They said that I have to use Maven to resolve dependencies. 他们说我必须使用Maven来解决依赖关系。 Now, here I am with all the archives and after 1 day, I still can't run a simple program. 现在,我拥有所有档案,并且在1天后,我仍然无法运行简单的程序。

In short I want to do this: 简而言之,我想这样做:

project 1: MG4J (automatically resolves dependencies, etc) 项目1:MG4J(自动解决依赖关系等)

project 2: MyProject (relies on MG4J) 项目2:MyProject(依赖MG4J)

MG4J uses Ant with Ivy (not Maven) as a build tool and dependency manager. MG4J使用Ant和Ivy(不是Maven)作为构建工具和依赖项管理器。 So, if you want to be able to modify the source and build it you are going to have to use the same tools. 因此,如果您希望能够修改和构建源,则必须使用相同的工具。

  1. Install Apache Ant, make a note of the installation directory (ANT_HOME) 安装Apache Ant,记下安装目录(ANT_HOME)
  2. Download an Apache Ivy binary distribution 下载Apache Ivy二进制发行版
  3. Unpack the Apache Ivy binary distribution to a folder, then copy ivy-2.xxjar to ANT_HOME/lib 将Apache Ivy二进制发行版解压缩到一个文件夹中,然后将ivy-2.xxjar复制到ANT_HOME / lib
  4. Download the source for MG4J . 下载MG4J的源代码 It comes as a JAR which you need to extract to a folder (MG4J_SRC). 它以JAR的形式出现,您需要将其提取到文件夹(MG4J_SRC)中。
  5. In the MG4J_SRC folder execute the command ant ivy-setupjars jar . 在MG4J_SRC文件夹中,执行命令ant ivy-setupjars jar If the build is successful, it will create a binary distribution for MG4J, as a JAR called mg4j-xxjar. 如果构建成功,它将为MG4J创建二进制发行版,名为JAR的mg4j-xxjar。

So, now you have a locally built version of the library, but you still can't use it (easily) in Maven. 因此,现在您有了库的本地构建版本,但是仍然无法(轻松)在Maven中使用它。 To make it accessible to your Mavenised project, you should install the library into your local Maven repo. 为了使您的Mavenised项目可以访问它,您应该将库安装到本地Maven存储库中。

mvn install:install-file  -Dfile={MG4J_SRC}/mg4j-x.x.jar
                          -DgroupId=your.groupId
                          -DartifactId=your.artifactId
                          -Dversion=your.version
                          -Dpackaging=jar
                          -DlocalRepositoryPath=path-to-specific-local-repo

Replace the your.groupId, your.artifactId, your.version and localRepositoryPath variables with values that make sense for your environment 用对您的环境有意义的值替换your.groupId,your.artifactId,your.version和localRepositoryPath变量

You can then add a dependency to the other project's POM: 然后,您可以将依赖项添加到另一个项目的POM中:

<dependency>
  <groupId>your.groupId</groupId>
  <artifactId>your-artifactId</artifactId>
  <version>your.version</version>
</dependency>

Now, as a final step, you want to make working with the MG4J source possible (and easy) from within Eclipse. 现在,作为最后一步,您希望在Eclipse中使用MG4J源成为可能(并且很容易)。 In your IDE, open File->New->Project and select 'Java Project from Existing Ant Buildfile' (type Ant in your Filter to narrow down the list). 在您的IDE中,打开“ File->New->Project然后选择“来自现有Ant构建File->New->Project的Java项目”(在“过滤器”中键入Ant以缩小列表范围)。

新蚂蚁项目

Click through the wizard and Finish. 单击向导并单击完成。 Now you have an Ant-based project in Eclipse, where you can easily modify the source and execute builds. 现在,您在Eclipse中有了一个基于Ant的项目,您可以在其中轻松地修改源代码并执行构建。 As a last step, you can change the deploy target in the MG4J build.xml file to install the MG4J into your local repository (as opposed to pushing it to Maven Central). 最后,您可以在MG4J build.xml文件中更改deploy目标,以将MG4J安装到本地存储库中(而不是将其推送到Maven Central)。

You have to add the MG4j dependency in your project pom file 您必须在项目pom文件中添加MG4j依赖项

<dependencies>
  [...]
  <dependency>
    <groupId>it.unimi.di</groupId>
    <artifactId>mg4j</artifactId>
    <version>5.2</version>
  </dependency>
</dependencies>

Try download the source code into your workspace directory, then in eclipse try Import->Maven->"Existing Maven Project" and point to source code directory of the project. 尝试将源代码下载到您的工作区目录中,然后在eclipse中尝试导入-> Maven->“现有Maven项目”,然后指向该项目的源代码目录。 Let me know if this works. 让我知道这个是否奏效。

mvn eclipse:eclipse将创建eclipse需要的垫片,然后创建项目,您应该会很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM