有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在使用maven的intelliJ中向Java项目添加scala依赖项

我想在纯Java项目中使用scala依赖项。没有必要在我的项目中编写scala,但当然我必须使用scala项目中定义的类/etc,我将其添加为依赖项。希望在IntellijMaven项目中执行此操作

我的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库中定义的包。基本上它找不到它们

我做错了什么


共 (1) 个答案

  1. # 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