maven建立java和scala混合的项目

项目需求:我们采用spark开发项目,使用的开发语言采用java和scala的混合,这个时候我们的项目需要支持java和scala,一般方法两种
(1)通过IDEA开发工具,下载SBT安装包,通过SBT创建项目,自动支持java和scala比较方便,但包的下载很慢
(2)项目我们使用IDEA开发工具,通过maven来完成java和scala混合项目

下面我们专门介绍如何通过maven来支持java和scala语言的项目,主要涉及的内容如下

1、执行创建语句(命令行模式下执行)

mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple  -DremoteRepositories=http://scala-tools.org/repo-releases 

执行过程中需要让您输入以下参数,根据步骤输入即可

Define value for property 'groupId': :
Define value for property 'artifactId': :
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  : :

输入完成后,通过回车结束,然后就可以创建成功了(紧scala项目)

2、项目结构

说明:

bigdata-user-profile 父目录(在pom.xml 中,通过model方式引入)

userprofile-db 子工程

userprofile-es 子工程

maven建立java和scala混合的项目_第1张图片

3、手动方式添加java目录(两个项目)

maven建立java和scala混合的项目_第2张图片

4、介绍一下3个工程pom.xml 配置

4.1 bigdata-user-profile 的pom.xml配置



    4.0.0
    com.demo.userprofile
    bigdata-user-profile
    pom
    1.0-SNAPSHOT
    
        userprofile-db
	userprofile-es
    

    
        UTF-8
        2.10.5
    

    
        
            org.apache.spark
            spark-core_2.10
            1.6.0
        

        
            org.apache.spark
            spark-sql_2.10
            1.6.0
        

        
            org.scala-lang
            scala-library
            ${scala.version}
            compile
        
        
            org.scala-lang
            scala-compiler
            ${scala.version}
            compile
        
        
            junit
            junit
            4.8.1
            test
        
    

    
        
            
                
                    net.alchim31.maven
                    scala-maven-plugin
                    3.2.1
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    2.0.2
                
            
        
        
            
                net.alchim31.maven
                scala-maven-plugin
                
                    
                        scala-compile-first
                        process-resources
                        
                            add-source
                            compile
                        
                    
                    
                        scala-test-compile
                        process-test-resources
                        
                            testCompile
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                        compile
                        
                            compile
                        
                    
                
            

            
                org.apache.maven.plugins
                maven-shade-plugin
                1.4
                
                    
                        package
                        
                            shade
                        
                        
                            
                                
                                    *:*
                                    
                                        META-INF/*.SF
                                        META-INF/*.DSA
                                        META-INF/*.RSA
                                    
                                
                            
                        
                    
                
            
        
    

4.2 user-profile-db 的pom.xml配置(user-profile-es 中pom.xml 同user-profile-db,不在介绍)



    
        com.demo.userprofile
        bigdata-user-profile
        1.0-SNAPSHOT
    

    4.0.0
    com.demo.userprofile
    userprofile-db
    1.0-SNAPSHOT
    ${project.artifactId}

    
        
            org.mongodb
            bson
            3.0.4
        
        
            org.mongodb
            mongo-java-driver
            3.0.4
        
        
            org.json
            json
            20141113
        
        
            com.google.code.gson
            gson
            2.3
        
    


5、编写测试代码

分别在java和scala目录下创建测试代码,主要测试打包是否完整,具体结构如图所示

maven建立java和scala混合的项目_第3张图片


6、执行maven的命令

mvn clean compile package

直到出现下面的命令表示成功

[INFO] Replacing D:\demo\user_profiles\bigdata-user-profile\userprofile-es\target\userprofile-es-1.0-SNAPSHOT.jar with D:\demo\user_profiles\bigdata-user-p
rofile\userprofile-es\target\userprofile-es-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] bigdata-user-profile ............................... SUCCESS [ 33.012 s]
[INFO] userprofile-db ..................................... SUCCESS [ 33.083 s]
[INFO] userprofile-es ..................................... SUCCESS [ 31.985 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:38 min
[INFO] Finished at: 2016-09-26T11:34:38+08:00
[INFO] Final Memory: 84M/857M
[INFO] ------------------------------------------------------------------------


7、通过编译工具查看是否把java和scala代码打包成功

实际测试工程中已经ok

maven建立java和scala混合的项目_第4张图片

到这里我们使用maven创建scala和java混合项目就成功了,以后再开发spark项目时候,就可以使用这种方式替代sbt方式了


你可能感兴趣的:(maven建立java和scala混合的项目)