在IJ中整合Java和Scala

1、首先需要下载scala的插件,要注意插件版本和IJ的版本比较接近。

下载地址:https://plugins.jetbrains.com/plugin/1347-scala

2、然后需要在maven的pom文件中配置编译scala代码的插件和scala的一些依赖包。

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6   <groupId>com.hxy</groupId>
  7   <artifactId>how2j</artifactId>
  8   <version>1.0-SNAPSHOT</version>
  9 
 10   <build>
 11     <plugins>
 12       <plugin>
 13         <!-- 编译java代码的 -->
 14         <groupId>org.apache.maven.plugins</groupId>
 15         <artifactId>maven-compiler-plugin</artifactId>
 16         <version>3.2</version>
 17         <configuration>
 18           <source>1.8</source>
 19           <target>1.8</target>
 20           <encoding>UTF-8</encoding>
 21         </configuration>
 22         <executions>
 23           <execution>
 24             <phase>compile</phase>
 25             <goals>
 26               <goal>compile</goal>
 27             </goals>
 28           </execution>
 29         </executions>
 30       </plugin>
 31       <plugin>
 32         <!-- 编译scala代码 -->
 33         <groupId>net.alchim31.maven</groupId>
 34         <artifactId>scala-maven-plugin</artifactId>
 35         <version>3.2.1</version>
 36         <executions>
 37           <execution>
 38             <id>scala-compile-first</id>
 39             <phase>process-resources</phase>
 40             <goals>
 41               <goal>add-source</goal>
 42               <goal>compile</goal>
 43             </goals>
 44           </execution>
 45         </executions>
 46       </plugin>
 47     </plugins>
 48   </build>
 49 
 50   <dependencies>
 51     <dependency>
 52       <groupId>junit</groupId>
 53       <artifactId>junit</artifactId>
 54       <version>3.8.1</version>
 55       <scope>test</scope>
 56     </dependency>
 57 
 58     <dependency>
 59       <groupId>junit</groupId>
 60       <artifactId>junit</artifactId>
 61       <version>4.11</version>
 62     </dependency>
 63 
 64     <dependency>
 65       <groupId>org.hibernate</groupId>
 66       <artifactId>hibernate-validator</artifactId>
 67       <version>4.3.2.Final</version>
 68     </dependency>
 69 
 70     <dependency>
 71       <groupId>javax.validation</groupId>
 72       <artifactId>validation-api</artifactId>
 73       <version>1.1.0.Final</version>
 74     </dependency>
 75 
 76     <dependency>
 77       <groupId>org.scala-lang</groupId>
 78       <artifactId>scala-library</artifactId>
 79       <version>2.10.1</version>
 80     </dependency>
 81 
 82     <dependency>
 83       <groupId>org.scala-lang</groupId>
 84       <artifactId>scala-library</artifactId>
 85       <version>2.12.1</version>
 86     </dependency>
 87     <dependency>
 88       <groupId>org.scala-lang</groupId>
 89       <artifactId>scala-compiler</artifactId>
 90       <version>2.12.1</version>
 91     </dependency>
 92     <dependency>
 93       <groupId>org.scala-lang</groupId>
 94       <artifactId>scala-reflect</artifactId>
 95       <version>2.12.1</version>
 96     </dependency>
 97     <dependency>
 98       <groupId>org.scala-lang</groupId>
 99       <artifactId>scala-actors</artifactId>
100       <version>2.11.8</version>
101     </dependency>
102     <dependency>
103       <groupId>org.scala-tools</groupId>
104       <artifactId>maven-scala-plugin</artifactId>
105       <version>2.15.2</version>
106     </dependency>
107 
108   </dependencies>
109 </project>

3、Java代码放在java目录下,Scala代码就放在scala代码下。一个简单的hello world例子如下。

4、可以在java代码中调用scala的代码。

5、总结

JVM的平台还是非常强大的,不管是Java、Kotlin还是Scala都各有所长,但是这些JVM上不同的语言,最终都可以编译成同一种格式的.class文件,且它们之间支持相互调用,因此不同的场景,我们可以

选择最适合的语言,然后由其他语言来进行调用,通过“胶水连接”来实现整个软件模块。

 

posted @ 2018-06-10 20:50  hxy_has_been_used  阅读(1330)  评论(0编辑  收藏  举报