zoukankan      html  css  js  c++  java
  • 在Eclipse中混合Java和Scala编程

    1. 新建项目目录 scala-java-mix

    2. 创建 src 目录及子目录:

    mkdir -p src/main/java
    mkdir -p src/main/scala

    3. 在目录 scala-java-mix 中创建 pom.xml文件,并添加下面内容到 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>lasclocker</groupId>
        <artifactId>scala-java-mix</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Scala-Java mixture</name>
        <description>Showcase mixing Scala and Java</description>
        <packaging>jar</packaging>
         
        <build>
            <plugins>
                <!-- ensure that we use JDK 1.6 -->
                <plugin>
                    <inherited>true</inherited>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                 
                <plugin>
                    <groupId>org.scala-tools</groupId>
                    <artifactId>maven-scala-plugin</artifactId>
                    <version>2.15.2</version>
                    <executions>
                        <!-- Run scala compiler in the process-resources phase, so that dependencies on
                             scala classes can be resolved later in the (Java) compile phase -->
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>                        
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            </execution>
     
                            <!-- Run scala compiler in the process-test-resources phase, so that dependencies on
                                 scala classes can be resolved later in the (Java) test-compile phase -->                    
                            <execution>
                            <id>scala-test-compile</id>
                            <phase>process-test-resources</phase>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                 
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <executions>
                        <!-- Add src/main/scala to source path of Eclipse -->
                        <execution>
                            <id>add-source</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/main/scala</source>
                                </sources>
                            </configuration>
                        </execution>
                           
                        <!-- Add src/test/scala to test source path of Eclipse -->
                        <execution>
                            <id>add-test-source</id>
                            <phase>generate-test-sources</phase>
                            <goals>
                                <goal>add-test-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/test/scala</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                 
                <!-- to generate Eclipse artifacts for projects mixing Scala and Java -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.8</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                        <projectnatures>
                            <projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
                            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
                        </projectnatures>
                        <buildcommands>
                            <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
                        </buildcommands>
                        <classpathContainers>
                            <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer>
                            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                        </classpathContainers>
                        <excludes>
                            <!-- in Eclipse, use scala-library, scala-compiler from the SCALA_CONTAINER rather than POM <dependency> -->
                            <exclude>org.scala-lang:scala-library</exclude>
                            <exclude>org.scala-lang:scala-compiler</exclude>
                        </excludes>
                        <sourceIncludes>
                            <sourceInclude>**/*.scala</sourceInclude>
                            <sourceInclude>**/*.java</sourceInclude>
                        </sourceIncludes>
                    </configuration>
                </plugin>
                 
                <!-- When run tests in the test phase, include .java and .scala source files -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.8.1</version>
                    <configuration>
                        <includes>
                            <include>**/*.java</include>
                            <include>**/*.scala</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
     
        <dependencies>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>2.9.0</version>
            </dependency>
        </dependencies>
    </project>
    View Code

    4. 在 eclipse 中 import 该项目,在目录 src/main/java 下创建 package,并编写 java 代码;在目录 src/main/scala 下创建 package,并编写 scala 代码

    5. 即可分别运行 java 代码 和 scala 代码

  • 相关阅读:
    redis全量复制和部分复制
    tp5怎么使用find_in_set
    ms1
    nginx+php上传大文件配置
    培训第一天!
    PHP中使用CURL(五)
    PHP中使用CURL(四)
    PHP中使用CURL(三)
    PHP中使用CURL(二)
    PHP中使用CURL(一)
  • 原文地址:https://www.cnblogs.com/lasclocker/p/5412251.html
Copyright © 2011-2022 走看看