maven打包scala程序

本文介绍了如何在Maven项目中使用scala-maven-plugin和maven-shade-plugin打包Scala程序,包括配置scala编译、处理签名冲突和确保主入口正确。还提到了解决执行jar包时可能遇到的AJNI错误和类找不到问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom文件打包scala程序

<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>org.example</groupId>
  <artifactId>项目名称</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>项目名称</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
  <plugins>
  <plugin>
    <!--专门用于scala项目构建和编译的插件,用于声明项目会利用scala-maven-plugin来执行scala的编译任务-->
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>4.8.1</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>
  <plugin>

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version> <!-- 使用最新版本替换 -->
    <executions>
      <execution>
        <phase>package</phase>

        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <!--一个小点 目的: 确保在构建 shaded JAR 时不会包含这些签名文件,避免执行jar包时出现SecurityExceptions安全问题  签名冲突-->
            <filters>
              <filter>
              <!-- Do not copy the signatures in the META-INF folder.
              Otherwise, this might cause SecurityExceptions when using the JAR. -->
          <artifact>*:*</artifact>
          <!-- 部分列出最终需要从jar包中排除的文件别表如下面三个文件-->
          <excludes>
            <exclude>META-INF/*.SF</exclude>
            <exclude>META-INF/*.DSA</exclude>
            <exclude>META-INF/*.RSA</exclude>
          </excludes>
        </filter>
        </filters>
      <!-- shade 插件的内置转化器从而你可以在生成的最终 shaded JAR 中设置 MANIFEST.MF 文件中的 Main-Class 属性指定程序的主入口 > mainClass元素-->
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>org.example.scala.test011</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
    <configuration>
    </configuration>
  </plugin>
        </plugins>
      </build>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.apache.spark</groupId>
          <artifactId>spark-streaming_2.12</artifactId>
          <version>3.0.0</version>
        </dependency>
        <!--引入spark-streaming 整合kafka的依赖-->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming-kafka-0-10_2.12</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.12</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.12</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.12.13</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>3.3.4</version>
    </dependency>
  </dependencies>
</project>

在执行jar包时关于Error: A JNI error has occurred, please check your installation and try again这个保存问题再确保

1.javac和java版本一致

2.pom文件中指定了mainClass元素确定了程序主入口

之后在idea中的Edit Configurations 中添加上个配置参数:

Add dependencies with“provided" scope to classpath

进而可以避免在执行jar包时由于缺少 provided 依赖而导致的类找不到问题而报错哦

配置后这里有上述箭头指向的配置项即可;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值