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

文章目录

  • 项目结构
  • Pom完整文件
  • 编译
  • 查看

实际开发用有时候引用自己写的一些java工具类,但是整个项目是scala开发的spark程序,在项目打包时需要考虑到java和scala混合在一起编译。
今天看到之前很久之前写的一些打包编译文章,发现很多地方不太对,于是重新整理更新如下。

项目结构

我们的项目结构可能如下图,既包含java的程序,也包含scala的程序。或者在scala的包中也包含了java程序。
Spark项目Java和Scala混合打包编译_第1张图片
实际开发中,我们可以不写src/main/java这个包,将java和scala程序全部放到src/main/scala中。

Pom完整文件

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


<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.0modelVersion>

    <groupId>com.kinggroupId>
    <artifactId>ggtoolartifactId>
    <version>1.0-SNAPSHOTversion>

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

    <dependencies>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-core_2.12artifactId>
            <version>3.3.0version>
        dependency>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-sql_2.12artifactId>
            <version>3.3.0version>
        dependency>
        <dependency>
            <groupId>org.scala-langgroupId>
            <artifactId>scala-libraryartifactId>
            <version>2.12.15version>
        dependency>
    dependencies>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resourcesdirectory>
            resource>
        resources>
        <plugins>
            
            <plugin>
                <groupId>net.alchim31.mavengroupId>
                <artifactId>scala-maven-pluginartifactId>
                <version>3.3.1version>
                <executions>
                    <execution>
                        <id>scala-compile-firstid>
                        <phase>process-resourcesphase>
                        <goals>
                            <goal>add-sourcegoal>
                            <goal>compilegoal>
                        goals>
                    execution>
                    <execution>
                        <phase>compilephase>
                        <goals>
                            <goal>compilegoal>
                            <goal>testCompilegoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    <scalaVersion>${scala.version}scalaVersion>
                    <args>
                        <arg>-target:jvm-1.8arg>
                    args>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                    <compilerArgument>-Xlint:uncheckedcompilerArgument>
                configuration>
            plugin>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-shade-pluginartifactId>
                <version>3.2.4version>
                <executions>
                    <execution>
                        <phase>packagephase>
                        <goals>
                            <goal>shadegoal>
                        goals>
                        <configuration><createDependencyReducedPom>falsecreateDependencyReducedPom>
                            <filters>
                                <filter>
                                    <artifact>*:*artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SFexclude>
                                        <exclude>META-INF/*.DSAexclude>
                                        <exclude>META-INF/*.RSAexclude>
                                    excludes>
                                filter>
                            filters>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

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

编译

这样在idea的右边工具栏中直接点击package即可完成打包。
Spark项目Java和Scala混合打包编译_第2张图片

Spark项目Java和Scala混合打包编译_第3张图片
在target的目录中,完整的包如下。
Spark项目Java和Scala混合打包编译_第4张图片

查看

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

Spark项目Java和Scala混合打包编译_第5张图片

你可能感兴趣的:(大数据,#,spark,spark,java,scala)