用maven编译、打包Java和Scala混合开发的项目

写在前面

摆脱依赖IDEA手动打jar包,实现maven管理spark\scala的不同版本的依赖,排除特定依赖,包含指

定依赖,并最终生成包含Java、Scala代码的混合编译的可执行jar包程序。

使用环境

  • jdk1.8
  • maven3.6.1
  • IDEA2017
  • spark-2.4.3
  • scala2.1.8

我的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.dr.leogroupId>
    <artifactId>FastKettleartifactId>
    <version>2.0.1version>
    <description>FastKettle2.0,零售商数据清洗系统!description>
    <url>https://www.jlpyyf.comurl>
    <developers>
        <developer>
            <id>leoid>
            <name>leo.jiename>
            <email>[email protected]email>
            <roles>
                <role>developerrole>
            roles>
        developer>
    developers>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <spark.version>2.4.3spark.version>
        <scala.version>2.11scala.version>
        <spark.cope>compilespark.cope>
    properties>

    <distributionManagement>
        
        <snapshotRepository>
            <id>snapshotsid>
            <url>http://192.168.x.xxx:8081/repository/maven-snapshots/url>
            
        snapshotRepository>
        <repository>
            <id>releasesid>
            <url>http://192.168.x.xxx:8081/repository/maven-releases/url>
        repository>
    distributionManagement>

    <dependencies>
        <dependency>
            
            <groupId>com.dr.leogroupId>
            <artifactId>dingtalk-smart-alertartifactId>
            <version>1.0.0-SNAPSHOTversion>
            <classifier>repackclassifier>
        dependency>
        <dependency>
            
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-core_${scala.version}artifactId>
            <version>${spark.version}version>
            <scope>providedscope>
            
        dependency>
        
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-sql_${scala.version}artifactId>
            <version>${spark.version}version>
            <scope>providedscope>
            
        dependency>

        
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-hive_${scala.version}artifactId>
            <version>${spark.version}version>
            <scope>providedscope>
            
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.32version>
        dependency>

        <dependency>
            <groupId>com.microsoft.sqlservergroupId>
            <artifactId>mssql-jdbcartifactId>
            <version>6.1.0.jre8version>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-toolsgroupId>
                <artifactId>maven-scala-pluginartifactId>
                <version>2.15.0version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compilegoal>
                            <goal>testCompilegoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    <scalaVersion>${scala.version}scalaVersion>
                    <args>
                        <arg>-target:jvm-1.5arg>
                    args>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-eclipse-pluginartifactId>
                <version>2.10version>
                <configuration>
                    <downloadSources>truedownloadSources>
                    <buildcommands>
                     <buildcommand>ch.epfl.lamp.sdt.core.scalabuilderbuildcommand>
                    buildcommands>
                    <additionalProjectnatures>
                        <projectnature>ch.epfl.lamp.sdt.core.scalanatureprojectnature>
                    additionalProjectnatures>
                    <classpathContainers>
                        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINERclasspathContainer>
                        <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINERclasspathContainer>
                    classpathContainers>
                configuration>
            plugin>
            <plugin>
                <artifactId>maven-assembly-pluginartifactId>
                <version>3.1.1version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependenciesdescriptorRef>
                    descriptorRefs>
                    
                    <finalName>FastKettle-2.0.1-RELEASEfinalName>
                    <appendAssemblyId>falseappendAssemblyId>
                configuration>
                <executions>
                    <execution>
                        <id>make-assemblyid>
                        <phase>packagephase>
                        <goals>
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.6.0version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>build-helper-maven-pluginartifactId>
                <version>1.10version>
                <executions>
                    <execution>
                        <id>add-sourceid>
                        <phase>generate-sourcesphase>
                        <goals>
                            <goal>add-sourcegoal>
                        goals>
                        <configuration>
                            
                            <sources>
                                <source>src/main/javasource>
                                <source>src/main/scalasource>
                            sources>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scala-toolsgroupId>
                <artifactId>maven-scala-pluginartifactId>
                <configuration>
                    <scalaVersion>${scala.version}scalaVersion>
                configuration>
            plugin>
        plugins>
    reporting>

project>

总结

以上方式,可能不是最完美的方式,但已经过实践可用,实现scala代码中调Java代码的功能模块,并实现最终的混合打包,执行。

你可能感兴趣的:(maven折腾记)