Spark项目Java和Scala混合打包编译

文章目录

实际开发用有时候引用自己写的一些java工具类,但是整个项目是scala开发的spark程序,在项目打包时需要考虑到java和scala混合在一起编译。

今天看到之前很久之前写的一些打包编译文章,发现很多地方不太对,于是重新整理更新如下。

项目结构

我们的项目结构可能如下图,既包含java的程序,也包含scala的程序。或者在scala的包中也包含了java程序。

实际开发中,我们可以不写src/main/java这个包,将java和scala程序全部放到src/main/scala中。

Pom完整文件

这是一个spark程序的完整的pom文件。

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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>com.king</groupId>
    <artifactId>ggtool</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <scala.version>2.12.15</scala.version>
        <spark.version>3.3.0</spark.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.12</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.12.15</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <!--解决java和scala混合编译出错-->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <args>
                        <arg>-target:jvm-1.8</arg>
                    </args>
                </configuration>
            </plugin>
            <!-- java compile-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArgument>-Xlint:unchecked</compilerArgument>
                </configuration>
            </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><createDependencyReducedPom>false</createDependencyReducedPom>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

scala-maven-plugin 用来打包scala程序,

maven-compiler-plugin 用来打包java程序。

编译

这样在idea的右边工具栏中直接点击package即可完成打包。

在target的目录中,完整的包如下。

查看

用压缩软件打开生成的jar包,可以看到java和scala的文件都编译在一起了。

相关推荐
睆小白7 分钟前
【springboot】整合沙箱支付
java·spring boot·后端
风槐啊24 分钟前
四、Java 基础语法
java·开发语言·markdown
原机小子30 分钟前
SpringBoot在校园健康管理中的应用
java·spring boot·后端
Supreme_Sir40 分钟前
Java 图片合成
java·图像处理·文本·二维码·图片合成·jai
梦幻通灵41 分钟前
Postman另存的curl脚本调试运行
java·开发语言·ide
OEC小胖胖43 分钟前
Spring Boot项目POM文件详解
java·spring boot·后端·spring
_GR44 分钟前
每日OJ题_牛客_JOR26最长回文子串_C++_Java
java·数据结构·c++·算法·动态规划
huapiaoy1 小时前
SpringBoot的配置文件
java·spring boot·后端
大道归简1 小时前
2.点位管理|前后端如何交互——帝可得后台管理系统
java·开发语言·git·gitee·github
CocoaAndYy1 小时前
Spring之生成Bean
java·前端·spring