为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
慕桂英3389331 2022-12-28 09:48:31
我有一个构建到 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 代码的源目录添加到要生成的源列表中,但显然不是。我怎样才能解决这个问题?
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

您正在为 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">

  <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>



查看完整回答
反对 回复 2022-12-28
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信