【问题标题】:How to add a scala dependency to a Java project in intelliJ that use maven如何在使用 maven 的 intelliJ 中向 Java 项目添加 scala 依赖项
【发布时间】:2020-08-06 10:21:28
【问题描述】:

我想在纯 Java 项目中使用 scala 依赖项。无需在我的项目中编写 scala,但当然我将不得不使用在我添加为依赖项的 scala 项目中定义的类/等。想在 intelliJ maven 项目中执行此操作。

我的 pom 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xxx</groupId>
    <artifactId>xxxx</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>group ID</groupId>
            <artifactId>scala dependancy</artifactId>
        </dependency>     
    </dependencies>

</project>

虽然我在 pom 中没有看到任何错误,但 intellij 无法导入 scala 库中定义的包。本质上它找不到它们。

我在这里做错了什么?

【问题讨论】:

  • 我不确定你到底需要什么(我不使用 Scala),但它可能看起来像这样:&lt;groupId&gt;org.scala-lang&lt;/groupId&gt; &lt;artifactId&gt;scala-library&lt;/artifactId&gt; &lt;version&gt;2.13.1&lt;/version&gt;(参见mvnrepository.com/artifact/org.scala-lang/scala-library

标签: java scala intellij-idea


【解决方案1】:

您可以通过将以下依赖项添加到您的 pom.xml 文件来做到这一点:

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.11.7</version>
    </dependency>
</dependencies>
<build>
    <plugins>
<!-- This plugin compiles Scala files -->
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
<!-- This plugin compiles Java files -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
<!-- This plugin adds all dependencies to JAR file during 'package' command.
Pay EXTRA attention to the 'mainClass' tag. 
You have to set name of class with entry point to program ('main' method) -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>ScalaRunner</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

查看this 了解更多信息。

【讨论】:

  • 我不知道为什么我需要这样的依赖项,因为没有理由在这里编译 scala 代码。但我会测试一下。
  • 很遗憾,它没有解决我的问题。
猜你喜欢
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 2020-01-03
  • 2020-12-06
  • 2012-10-22
  • 2012-12-30
相关资源
最近更新 更多