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 子工程

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


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

4.1 bigdata-user-profile 的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo.userprofile</groupId>
    <artifactId>bigdata-user-profile</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>userprofile-db</module>
	<module>userprofile-es</module>
    </modules>

    <properties>
        <encoding>UTF-8</encoding>
        <scala.version>2.10.5</scala.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>1.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.10</artifactId>
            <version>1.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-compiler</artifactId>
            <version>${scala.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <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-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <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>

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

<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/maven-v4_0_0.xsd">

    <parent>
        <groupId>com.demo.userprofile</groupId>
        <artifactId>bigdata-user-profile</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo.userprofile</groupId>
    <artifactId>userprofile-db</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>

    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>bson</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>
</project>

5、编写测试代码

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



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创建scala和java混合项目就成功了,以后再开发spark项目时候,就可以使用这种方式替代sbt方式了


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾文教编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值