Maven:将 Scala 代码和 Java 代码一起构建到一个 Fat JAR 中

我有一个构建到 Fat JAR 中的 Scala 项目。今天我需要向项目添加一些 Java 类,但现在我的 Maven 构建失败了。


我的项目结构(大致)如下所示:


.

├── src

│   └── main

│       ├── resources

│       │   └── Log4j.properties

|       ├── java

│       │   └── com

│       │       └── myorg

│       │           └── myproject

│       │               └── MyPublicJavaClass.java

│       └── scala

│           └── com

│               └── myorg

│                   └── myproject

│                       └── spark

│                           └── Main.scala

└── pom.xml

在我添加 Java 类之前,以及build-helper-maven-plugin今天,Maven 能够毫无问题地构建这个项目。但是现在,似乎我没有正确配置该插件,或者我没有使用正确的插件?

我的 Scala 代码试图使用类型的对象,MyPublicJavaClass所以现在我在 Maven 中看到的构建错误如下所示:

[错误] ~/src/main/scala/com/myorg/myproject/spark/Main.scala:227: error: not found: type MyPublicJavaClass

...

[错误] 无法在项目 myproject 上执行目标 org.scala-tools:maven-scala-plugin:2.15.2:compile (default): wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (退出值: 1) -> [帮助 1]

我以为它build-helper-maven-plugin会告诉它在编译时之前将 Java 代码的源目录添加到要生成的源列表中,但显然不是。我怎样才能解决这个问题?


慕桂英3389331
浏览 84回答 1
1回答

森栏

您正在为 Scala 编译使用一个非常旧的插件(最新版本2.15.2已于 2011年 2 月 6 日发布)。我建议您先升级到更新的插件,例如( 2019 年 5 月 11 日的scala-maven-plugin最新版本)。4.0.2然后,您可以在文档中找到混合 Scala/Java 源代码的示例。在这种情况下不需要使用build-helper-maven-plugin,也不需要配置sourceDirectory和testSourceDirectory。用这个插件检查这个简单pom.xml的(当我在本地重现问题时,我刚刚从你提供的示例中删除了未使用的依赖项):<?xml version="1.0"?><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">&nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; <name>myproject</name>&nbsp; <url>http://maven.apache.org</url>&nbsp; <groupId>com.myorg</groupId>&nbsp; <artifactId>myproject</artifactId>&nbsp; <packaging>jar</packaging>&nbsp; <version>0.1.0-RELEASE</version>&nbsp; <properties>&nbsp; &nbsp; <maven.compiler.source>1.8</maven.compiler.source>&nbsp; &nbsp; <maven.compiler.target>1.8</maven.compiler.target>&nbsp; &nbsp; <encoding>UTF-8</encoding>&nbsp; &nbsp; <scala.tools.version>2.11</scala.tools.version>&nbsp; &nbsp; <scala.version>2.11.8</scala.version>&nbsp; </properties>&nbsp; <build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>net.alchim31.maven</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>scala-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>4.0.2</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>scala-compile-first</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>process-resources</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>add-source</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>compile</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>scala-test-compile</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>process-test-resources</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>testCompile</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; <!-- This builds the fat JAR -->&nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>com.myorg.myproject.spark.Main</mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRef>jar-with-dependencies</descriptorRef>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>make-assembly</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>single</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins>&nbsp; </build>&nbsp; <dependencies>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; <groupId>org.scala-lang</groupId>&nbsp; &nbsp; &nbsp; <artifactId>scala-library</artifactId>&nbsp; &nbsp; &nbsp; <version>${scala.version}</version>&nbsp; &nbsp; &nbsp; <scope>provided</scope>&nbsp; &nbsp; </dependency>&nbsp; </dependencies></project>
打开App,查看更多内容
随时随地看视频慕课网APP