0

我正在尝试在 IntelliJ IDEA 上构建一个 Scala-Maven 项目。创建项目后,它说构建成功。这就是我的 pom.xml 的样子:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dbloads.pgms</groupId>
  <artifactId>Arts</artifactId>
  <version>1.0-SNAPSHOT</version>
  <inceptionYear>2008</inceptionYear>
  <properties>
    <scala.version>2.7.0</scala.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.specs</groupId>
      <artifactId>specs</artifactId>
      <version>1.2.5</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
          <groupId>net.alchim31.maven</groupId>
          <artifactId>scala-maven-plugin</artifactId>
          <version>3.4.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
          <args>
            <arg>-target:jvm-1.5</arg>
          </args>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <downloadSources>true</downloadSources>
          <buildcommands>
            <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
          </buildcommands>
          <additionalProjectnatures>
            <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
          </additionalProjectnatures>
          <classpathContainers>
            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
            <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
          </classpathContainers>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.4.0</version>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>

接下来我尝试在项目中添加编译器选项,如下所示: 在此处输入图像描述

添加后,当我单击 RUN 按钮时,我收到以下错误消息:

[错误] 插件 net.alchim31.maven:scala-maven-plugin:3.4.0 或其依赖项之一无法解析:无法读取 net.alchim31.maven:scala-maven-plugin:jar:3.4 的工件描述符.0:无法将工件 net.alchim31.maven:scala-maven-plugin:pom:3.4.0 从/到中央传输(https://repo.maven.apache.org/maven2):PITC-Zscaler-EMEA- Amsterdam3PR.proxy.corporate.ge.com:未知主机 PITC-Zscaler-EMEA-Amsterdam3PR.proxy.corporate.ge.com -> [帮助 1]

我正在使用jdk version: 1.8.0_172并且我直接从插件中添加了 Scala 插件。因此它是 Scala 的最新版本。谁能让我知道如何解决这个问题。

4

1 回答 1

3

看起来您需要配置MavenIntelliJ以使用网络代理设置,因为看起来您可能位于公司防火墙后面。

Maven 能够通过其设置(在~/.m2/settings.xmlUnix系统或Windows%HOME%\.m2\settings.xml上)配置代理,如下所示:

<settings ...>
  .
  .
  <proxies>

    <!-- You can have one of these for each possible proxy. -->
    <proxy>
      <active>true</active>

      <!-- Pick some ID for your proxy here. -->
      <id>corp-proxy</id>

      <!-- Choose your protocol here. E.g. "http", "socks4" or "socks5" -->
      <protocol>http</protocol>

      <!-- Specify the proxy server name (or IP address) and port of your proxy here. -->
      <host>proxy.example.com</host>
      <port>8080</port>

      <!-- Identify any hosts here that you can access directly. It's unlikely that you'll
      need this unless you have a proxy repository (such as Nexus, Artifactory, etc.) on
      your corporate network. -->
      <nonProxyHosts>www.google.com|*.example.com</nonProxyHosts>

      <!-- The following fields are only necessary if required by your proxy. If you need to
      enter your own username and password, make sure you do not add this file to version
      control! -->
      <username>proxyuser</username>
      <password>somepassword</password>
    </proxy>
  </proxies>
  .
  .
</settings>

同时,IntelliJ被配置为通过其设置使用代理。有关详细信息,请参阅此答案。(请注意,通过JAVA_OPTS环境变量设置代理信息将适用于运行需要通过代理访问Internet的任何Java / Scala / JVM应用程序。)

或者,如果您的代理设置配置正确或不需要,则可能是暂时的网络连接问题,因此请确保您有Internet连接,然后重试。(例外是MavenMaven中央存储库下载插件失败。)

顺便说一句,您指定的Scala版本 - 2.7.0 - 是古老的,几乎可以肯定不适用于JDK 8。请使用较旧的JDK或更新版本的Scala(当前版本是 2.12.6)。

请注意,如果您需要使用当前版本的Apache Spark,您当前必须使用Scala 2.11.x - 最新版本是 2.11.12。

更新

从您的评论来看,似乎对Mavenscala-maven-pluginIntelliJIntelliJ Scala插件所扮演的角色有些混淆,所以我将在这里快速总结它们。如果我涉及您已经熟悉的主题,请多多包涵……

Maven是一个用于构建和发布软件的系统。(它实际上做的远不止这些,这就是Maven将自己描述为项目管理软件的原因。)它允许开发人员在一个地方指定他们软件的所有依赖项(软件使用的第三方库),Maven根据需要从中央Maven 存储库(主要是开源存储库)或其他私有存储库(根据需要)下载。进一步的设置控制如何配置编译器、运行测试、生成报告等。

Maven主要是为开发Java语言项目而开发的。scala-maven-plugin为Maven中的Scala语言和编译器提供支持。正是这个插件下载了您项目指定的Scala编译器版本,并使用它来编译和构建您的源代码。

如果您查看Maven项目的pom.xml文件,您会注意到该build部分中的以下几行:

<project ...>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.4.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
          <args>
            <arg>-target:jvm-1.5</arg>
          </args>
        </configuration>
      </plugin>
      ...
  </build>
  ...
</project>

并再次在该reporting部分:

<project ...>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.4.0</version>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>

在这两种情况下,都有一行读取<scalaVersion>${scala.version}</scalaVersion>,它告诉Maven你想使用哪个版本的Scala 。然后插件使用这个版本的Scala编译器,并让Maven将其下载到您机器上的缓存本地存储库(通常在C:\Users\{your account}\.m2Windows机器上)。请注意,Mavenscala-maven-plugin都将忽略您在机器上安装的任何版本的Scala 。

那么该插件将为您下载哪个版本的Scala ?该值${scala.version}表明版本号存储为名为 的属性的值scala.version。您的pom.xml文件在靠近顶部的位置也有创建此属性的行:

<project ...>
  ...
  <properties>
    <scala.version>2.7.0</scala.version>
  </properties>
  ...
</project>

因此,您可以看到您将使用Scala编译器的 2.7.0 版本。如果您想使用最新的Scala版本,您应该将其更改为:

<project ...>
  ...
  <properties>
    <scala.version>2.12.6</scala.version>
  </properties>
  ...
</project>

好的,现在您将使用最新版本的Scala编译器。现在让我们继续使用IntelliJ ...

IntelliJ IDEA是一个集成开发环境( IDE ),主要用于使用Java语言进行开发。它提供语法高亮、智能代码完成和其他功能来简化编写代码的过程。为了为Scala编程语言提供这些功能,您需要安装其Scala插件。

您可以将IntelliJ配置为使用您机器上安装的任何版本的Scala 。IntelliJ将知道如何编译、构建和运行您的软件,并且可以在不使用Maven 项目对象模型( POM ) 文件的构建定义的情况下工作。

然而,使用Maven的原因之一是为了确保开发项目的构建环境一致,因此它不是每个开发人员可能在他们的机器上安装或可能没有安装的任何东西的心血来潮。因此,如果项目使用Maven,最好告诉IntelliJ。这样,IntelliJ可以使用Mavenpom.xml文件来指定编译器的版本、下载依赖项、配置编译器设置等。

因此,上述信息应该可以帮助您启动并运行您的项目,使用您公司的网络代理并使用最新版本的Scala,使用MavenIntelliJ

于 2018-06-16T17:58:26.500 回答