如何找到正确的 Maven 原型项目,以便在 Eclipse 中使用 Scala 进行开发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29114936/
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-09-09 05:31:16  来源:igfitidea点击:

How do I find the correct Maven archetype project for developing with Scala in Eclipse?

eclipsescalamavenm2eclipsemaven-archetype

提问by Jane Wayne

I'm having problems trying to just create a Maven project using Scala (v2.11.6) within Eclipse (Luna). The instructions heresays to install both plugins below.

我在尝试在 Eclipse (Luna) 中使用 Scala (v2.11.6) 创建 Maven 项目时遇到问题。这里的说明说要安装下面的两个插件。

  • Maven Integration in Eclipse
  • m2eclipse-scala connector
  • Eclipse 中的 Maven 集成
  • m2eclipse-scala 连接器

Both plugins can be installed by using the m2eclipse-scala update site. So here's what I did in my first attempt.

可以使用m2eclipse-scala 更新站点安装这两个插件。所以这就是我第一次尝试时所做的。

  • Download Eclipse Luna (for JavaEE developers)
  • Install "both" plugins above by using the m2eclipse-scala update site.
  • 下载 Eclipse Luna(适用于 JavaEE 开发人员)
  • 使用 m2eclipse-scala 更新站点安装上面的“两个”插件。

When Eclipse loads up, I attempt to create a Maven project, and the archetype I want to use is Group Id=net.alchim31.maven, Artifact Id=scala-archetype-simple, Version=1.5. However, this archetype never shows up when I enter in "scala-arch" in the filter text field.

当Eclipse加载时,我尝试创建一个Maven项目,我想使用的原型是Group Id=net.alchim31.maven,Artifact Id=scala-archetype-simple,Version=1.5。但是,当我在过滤器文本字段中输入“scala-arch”时,这个原型永远不会出现。

The only project I see is the one with Group Id=org.scala-tools.archetypes, Artifact Id=scala-archetype-simple, Version=1.2. When I select this archetype to use, I get a bunch of error messages in Eclipse.

我看到的唯一一个项目是 Group Id=org.scala-tools.archetypes,Artifact Id=scala-archetype-simple,Version=1.2。当我选择使用这个原型时,我在 Eclipse 中收到一堆错误消息。

  • error while loading ConsoleRunner, Scala signature ConsoleRunner has wrong version expected: 5.0 found: 4.1 in ConsoleRunner.class
  • error while loading JUnit4, Scala signature JUnit4 has wrong version expected: 5.0 found: 4.1 in JUnit4.class
  • error while loading Specification, Scala signature Specification has wrong version expected: 5.0 found: 4.1 in Specification.class
  • 加载 ConsoleRunner 时出错,Scala 签名 ConsoleRunner 预期版本错误:5.0 发现:ConsoleRunner.class 中的 4.1
  • 加载 JUnit4 时出错,Scala 签名 JUnit4 预期版本错误:5.0 发现:JUnit4.class 中的 4.1
  • 加载规范时出错,Scala 签名规范预期版本错误:5.0 发现:Specification.class 中的 4.1

In my second attempt, I try to download the pre-packaged bundle (Scala IDE for Eclipse). Again, when I attempt to create a Scala Maven project, I don't see the Maven archetype for Group Id=net.alchim31.maven (only for Group Id=org.scala-tools.archetypes).

在我的第二次尝试中,我尝试下载预先打包的包(用于 Eclipse 的 Scala IDE)。同样,当我尝试创建 Scala Maven 项目时,我没有看到 Group Id=net.alchim31.maven(仅适用于 Group Id=org.scala-tools.archetypes)的 Maven 原型。

Any ideas on what I'm doing wrong here? Or where I can find a vanilla Scala Maven project to import/modify and use for my own purpose?

关于我在这里做错了什么的任何想法?或者我在哪里可以找到一个香草 Scala Maven 项目来导入/修改并用于我自己的目的?

回答by Paul Samsotha

Here's how I got it to work. It seems all the archetypes aren't available, so based on this answer, I created a remote catalog

这是我如何让它工作的。似乎所有的原型都不可用,所以根据这个答案,我创建了一个远程目录

Before

Image showing the original dialog with missing entry

显示缺少条目的原始对话框的图像

So do this

所以这样做

Go to [Windows] → [Preferences] → [Maven] → [Archetypes] → Add Remote Catalogand create the catalog, then Apply

转到[Windows] → [首选项] → [Maven] → [Archetypes] →Add Remote Catalog并创建目录,然后Apply

Image showing "Remote Archetype Catalog" modal window

显示“远程原型目录”模式窗口的图像

For copy-pasting:

对于复制粘贴:

Go back to creating a new Maven project. Select the Remote Catalogfrom the dropdown. It will take a few moments to gather all the archetypes. You can see the progress at the very bottom right of the IDE. When it's done, you should be able to see the archetypes

返回创建一个新的 Maven 项目。Remote Catalog从下拉列表中选择。收集所有原型需要一些时间。您可以在 IDE 的右下角看到进度。完成后,您应该能够看到原型

After

Image showing "scala-archetype" entry in the dialog

显示对话框中“scala-archetype”条目的图像

Create your project with groupId and artifactId and such.

使用 groupId 和 artifactId 等创建您的项目。

Right click the project, then from the context menu [Run As] &rarr [Maven Build]. In the dialog, type clean packageinto the goals. The run. The project should build and run the tests.

右键单击项目,然后从上下文菜单 [Run As] &rarr [Maven Build]。在对话框中,输入clean package目标。运行。该项目应该构建并运行测试。

For me at first I got an error on the build. It was because my default environment is using Java 8. I'm pretty new to Scala, so I'm not sure is there is a problem with Scala and Java 8 or not (I think it's the Scala version in the pom (2.10.0)). But what I did to get it to work was just change the Java version used in the IDE to Java 7.

对我来说,起初我在构建时遇到了错误。这是因为我的默认环境使用的是 Java 8。我对 Scala 还很陌生,所以我不确定 Scala 和 Java 8 是否存在问题(我认为这是 pom (2.10.x) 中的 Scala 版本)。 0))。但是我所做的只是将 IDE 中使用的 Java 版本更改为 Java 7。

Basically just go to [Windows] → [Preferenes] → [Java] → [Installed JREs] → Add→ [System VM] → Next→ Directory → Find the directory of the Java home (version 7) → Finish→ Then in the list tick Java 7.

基本上只需转到 [Windows] → [Preferenes] → [Java] → [已安装的 JRE] → Add→ [系统 VM] → Next→ 目录 → 找到 Java 主目录(版本 7)→ Finish→ 然后在列表中勾选 Java 7 .

Then build again. It should work. Good Luck!

然后再次构建。它应该工作。祝你好运!

回答by Deepak Acharya

One point I want to add, If you are getting following error while installing the archetype showed in image 1

我想补充一点,如果您在安装图 1 中显示的原型时遇到以下错误

Can't resolve Archetype org.glassfish.jersey.archetypes:jersey-quickstart-webapp:2.21 org.eclipse.core.runtime.CoreException: Could not resolve artifact org.glassfish.jersey.archetypes:jersey-quickstart-webapp:pom:2.21

无法解析 Archetype org.glassfish.jersey.archetypes:jersey-quickstart-webapp:2.21 org.eclipse.core.runtime.CoreException: 无法解析工件 org.glassfish.jersey.archetypes:jersey-quickstart-webapp:pom: 2.21

Image 1: enter image description here

图 1: 在此处输入图片说明

Then before procedding with the solution specified above, try to complete these stepsin eclipse.

然后在继续使用上面指定的解决方案之前,尝试在 eclipse 中完成这些步骤

回答by Viliam Simko

After you have successfully created the scala-maven project as described above, you will probably see an error "Type not found: type JUnitRunner specs.scala". In that case, manually add the following XML snippet to your pom.

如上所述成功创建 scala-maven 项目后,您可能会看到错误“找不到类型:键入 JUnitRunner specs.scala”。在这种情况下,手动将以下 XML 片段添加到您的 pom.xml 文件中。

<dependency>
    <groupId>org.specs2</groupId>
    <artifactId>specs2-junit_${scala.compat.version}</artifactId>
    <version>2.4.16</version>
    <scope>test</scope>
</dependency>

Moreover, to avoid errors when running mvn test,

此外,为了避免运行时出错mvn test

[ERROR] scalac error: bad option: '-make:transitive'

You also need to delete the following line from the pom.xmlfile:

您还需要从pom.xml文件中删除以下行:

<arg>-make:transitive</arg>

回答by Nirmalya

I faced the same problem and chanced upon this SO question, as well as the answer, given by @peeskillet (many thanks). I am using:

我遇到了同样的问题,偶然发现了这个问题,以及@peeskillet 给出的答案(非常感谢)。我在用:

  • Maven 3.3.3
  • Eclipse Luna Scala IDE 4.0.0.-vfinal-20150305-1644-Typesafe
  • Scala 2.11.6
  • JDK 1.8
  • Maven 3.3.3
  • Eclipse Luna Scala IDE 4.0.0.-vfinal-20150305-1644-Typesafe
  • 斯卡拉 2.11.6
  • JDK 1.8

After following the steps exactly as given here, I hit a wall. When I chose the right archetype (net.alchim31.maven), I was greeted with this error: Could not resolve artifact net.alchim31.maven:scala-archetype-simple:pom:1.5Suspecting that the maven embedded in eclipse was acting funny, I visited [Windows] -> [Maven] -> [Installations] and added local installation of maven (whatever is set to M2_HOME in my shell). But, no luck!

完全按照此处给出的步骤进行操作后,我碰壁了。当我选择了正确的原型(net.alchim31.maven)时,我遇到了这个错误:Could not resolve artifact net.alchim31.maven:scala-archetype-simple:pom:1.5怀疑嵌入在 eclipse 中的 maven表现得很 有趣,我访问了 [Windows] -> [Maven] -> [Installations] 并添加了本地安装 maven(在我的 shell 中设置为 M2_HOME 的任何内容)。但是,没有运气!

Exasperated, I visited this site: Scala Docand switched back to command-line as this page instructs. Surprisingly (to me, at least), it worked. A maven-Scala project was created in the present directory, which I could import into Eclipse.

气得我访问了这个网站:Scala Doc并按照这个页面的指示切换回命令行。令人惊讶的是(至少对我而言),它奏效了。在当前目录中创建了一个 maven-Scala 项目,我可以将其导入 Eclipse。

Just so that I am not misunderstood, I am not suggesting that @peeskillet's answer is wrong. Just wanted to share another way of creating a maven-scala project on Eclipse, in case it helps someone. I have not yet understood, why Eclipse-Scala-IDE is yet to give that 'Aha' feeling.

为了不被误解,我并不是在暗示@peeskillet 的回答是错误的。只是想分享另一种在 Eclipse 上创建 maven-scala 项目的方法,以防它对某人有所帮助。我还不明白,为什么 Eclipse-Scala-IDE 还没有给人一种“啊哈”的感觉。