我有一个Scala项目,我将其构建到Fat JAR中。今天,我需要向项目中添加一些Java类,但是现在我的Maven构建失败了。
我的项目结构看起来(大致)如下:
.
├── src
│ └── main
│ ├── resources
│ │ └── Log4j.properties
| ├── java
│ │ └── com
│ │ └── myorg
│ │ └── myproject
│ │ └── MyPublicJavaClass.java
│ └── scala
│ └── com
│ └── myorg
│ └── myproject
│ └── spark
│ └── Main.scala
└── pom.xml
这是我的POM文件的样子:
<?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">
<modelVersion>4.0.0</modelVersion>
<name>myproject</name>
<url>http://maven.apache.org</url>
<groupId>com.myorg</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>0.1.0-RELEASE</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.tools.version>2.11</scala.tools.version>
<scala.version>2.11.8</scala.version>
<spark.version>2.4.0</spark.version>
<aws.sdk.version>1.11.553</aws.sdk.version>
</properties>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<!-- I added this plugin today to try and make it compile the Java code -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<sources>
<source>src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- This compiles the Scala code -->
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- This builds the fat JAR -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myorg.myproject.spark.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.tools.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.tools.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.amazon.emr</groupId>
<artifactId>emr-dynamodb-hadoop</artifactId>
<version>4.8.0</version>
</dependency>
<dependency>
<groupId>com.github.scopt</groupId>
<artifactId>scopt_${scala.tools.version}</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-scala</artifactId>
<version>11.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在我添加Java类以及今天的build-helper-maven-plugin
之前,Maven能够毫无问题地构建此项目。但是现在,看来我没有正确配置该插件,或者也许我没有使用正确的插件?
我的Scala代码正在尝试使用MyPublicJavaClass
类型的对象,因此现在我在Maven中看到的构建错误看起来像这样:
[错误]〜/ src / main / scala / com / myorg / myproject / spark / Main.scala:227:错误:找不到:输入MyPublicJavaClass
...
[错误]无法在项目myproject上执行目标org.scala-tools:maven-scala-plugin:2.15.2:compile(默认):包装:org.apache.commons.exec.ExecuteException:流程退出错误:1(退出值:1)-> [帮助1]
我认为build-helper-maven-plugin
会告诉它将Java代码的源目录添加到要在编译之前生成的源列表中,但是显然没有。我该如何解决?
答案 0 :(得分:1)
您正在使用非常老的插件进行Scala编译(最新版本2.15.2
已发布Feb 6 2011)。
我建议您首先升级到更新的插件,例如scala-maven-plugin
(最新发布的4.0.2
于2019年5月11日)。
然后您可以找到一个包含Scala / Java混合源in the docs的示例。在这种情况下,无需使用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">
<modelVersion>4.0.0</modelVersion>
<name>myproject</name>
<url>http://maven.apache.org</url>
<groupId>com.myorg</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>0.1.0-RELEASE</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.tools.version>2.11</scala.tools.version>
<scala.version>2.11.8</scala.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.0.2</version>
<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 builds the fat JAR -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myorg.myproject.spark.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>