Maven无法正确构建Scala项目

时间:2019-04-17 07:03:07

标签: scala maven intellij-idea

问题

我有一个Maven-Scala项目。当我想在IntelliJ中构建它时,它会进行编译等。可以工作,但是输出 jar为空,除了pom.xml。 当使用mvn scala:compile通过命令行构建整个项目时,出现以下错误:

[INFO] Compiling 67 source files to /Users/my.name/Documents/company/util/util/target/classes at 1555483575272
[ERROR] error: scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found.
[ERROR]     at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:17)
[ERROR]     at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:18)
[ERROR]     at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:53)
[ERROR]     at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:45)
[ERROR]     at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:45)
[ERROR]     at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:66)
[ERROR]     at scala.reflect.internal.Mirrors$RootsBase.getClassByName(Mirrors.scala:102)
[ERROR]     at scala.reflect.internal.Mirrors$RootsBase.getRequiredClass(Mirrors.scala:105)
[ERROR]     at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass$lzycompute(Definitions.scala:257)
[ERROR]     at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass(Definitions.scala:257)
[ERROR]     at scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1394)
[ERROR]     at scala.tools.nsc.Global$Run.<init>(Global.scala:1215)
[ERROR]     at scala.tools.nsc.Driver.doCompile(Driver.scala:31)
[ERROR]     at scala.tools.nsc.MainClass.doCompile(Main.scala:23)
[ERROR]     at scala.tools.nsc.Driver.process(Driver.scala:51)
[ERROR]     at scala.tools.nsc.Driver.main(Driver.scala:64)
[ERROR]     at scala.tools.nsc.Main.main(Main.scala)
[ERROR]     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR]     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.base/java.lang.reflect.Method.invoke(Method.java:564)
[ERROR]     at scala_maven_executions.MainHelper.runMain(MainHelper.java:164)
[ERROR]     at scala_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.256 s
[INFO] Finished at: 2019-04-17T08:46:16+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.3.1:compile (default-cli) on project util: wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) 

我也可以运行mvn compile,但是成功,但是会产生一个空的jar:

[INFO] Building util 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/markus.geilehner/Documents/Syniverse/util/util/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ util ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to /Users/markus.geilehner/Documents/Syniverse/util/util/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.334 s
[INFO] Finished at: 2019-04-17T09:03:55+02:00
[INFO] ------------------------------------------------------------------------

项目文件结构

-util
--src
---main
----scala
-----com.company.util
------Scala Classes and packages

pom.xml

这是pom.xml的某些部分。我使用了 net.alchim Scala-maven插件。 构建过程配置为使用scala目录。

<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.3.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <artifactId>maven-project-info-reports-plugin</artifactId>
                            <version>2.2</version>
                        </plugin>
                        <plugin>
                            <groupId>net.alchim31.maven</groupId>
                            <artifactId>scala-maven-plugin</artifactId>
                            <version>3.3.1</version>
                            <configuration>
                                <jvmArgs>
                                    <jvmArg>-Xms64m</jvmArg>
                                    <jvmArg>-Xmx1024m</jvmArg>
                                </jvmArgs>
                            </configuration>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

1 个答案:

答案 0 :(得分:3)

解决方案

在将scala项目与maven结合使用时,我遇到了相同的错误。

问题1

一个问题可能是您使用了错误的Java版本(9/10)尝试将其更改为IntelliJ中的Java 8(1.8)(文件=>项目结构)。

问题2

在您的pom中,在maven-scala插件下方添加以下执行。

<executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <phase>test-compile</phase>
                    </execution>
                    <execution>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>

全新安装,然后重试。