eclipse一直用,用惯了,懒得换idea编辑器,就想着在以前的java web 的项目上改用kotlin语言编写
想学新的语言kotlin,想学maven管理工具,想现学现用,就自己去找博客论坛去配环境玩
但是苦于国内的关于这种配置的博客太少,花了半天多的时间,算是完成了,可以用kotlin写web项目玩了
(首先我写的不一定对,有错要说出来)
首先要确定版本的问题,java 用JDK 1.8以上的,要不然kotlin插件安装不上(我是这样的),tomcat最好用新一点,和JDK版本保持一致,要不然就会出现编译的class和发布的class的文件版本不一致的问题,gradle有二进制和完整版两种文件,建议使用完整版,maven更适合于eclipse,建议使用maven
说正事:gradle环境搭建:http://codemcx.blog.51cto.com/9638142/1936567
maven环境搭建:http://www.mamicode.com/info-detail-1855085.html(转载)
转载的是gradle,maven的这些插件的使用方式
我要说一下我的配置
也就是pom.xml的配置,这是最大的难点,一点一点解释
<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">
这一段是maven的pom.xml头部,xml解析时必须要的部分
这一段是基本信息,groupId是指项目所在的组,而artifactld指的是项目名,packaging指的是项目打包成什么类型的文件
<build><!-- 只编译Kotlin时配置 --><!-- <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory><testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> -->
<plugins><!-- Kotlin编译插件必须配置在Maven编译插件之前 --><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><version>${kotlin.version}</version><executions><execution><id>compile</id><goals><goal>compile</goal></goals><!--Kotlin与Java代码混编时指定资源目录
--><configuration><sourceDirs><sourceDir>${project.basedir}/src/main/kotlin</sourceDir><sourceDir>${project.basedir}/src/main/java</sourceDir></sourceDirs></configuration></execution><execution><id>test-compile</id><goals><goal>test-compile</goal></goals><configuration><sourceDirs><sourceDir>${project.basedir}/src/test/kotlin</sourceDir><sourceDir>${project.basedir}/src/test/java</sourceDir></sourceDirs></configuration></execution></executions></plugin>接下来的这段是编译了如果之编译kotlin第一段写上,当然不建议接下来写plugin(插件)指定了kotlin代码的编译位置,以及maven和kotlin结合的插件自己想指定自己的目录都随意
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.5.1</version><configuration><!--<encoding>utf-8</encoding>
--><source>1.7</source><target>1.7</target></configuration><executions><!--替换会被 maven 特别处理的 default-compile
--><execution><id>default-compile</id><phase>none</phase></execution><!--替换会被 maven 特别处理的 default-testCompile
--><execution><id>default-testCompile</id><phase>none</phase></execution><execution><id>java-compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>java-test-compile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals></execution></executions></plugin>
这一段是maven自己指定编译的插件
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.6</version><configuration><warName>maven</warName></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><server>tomcat7</server><path>/maven</path><!--应用的部署context path
--></configuration></plugin></plugins></build></project>这一段是自己指定打包为war包的插件关于pom.xml的配置就是如此如果需要新的依赖或者jar包可以自己导入或者自己写依赖kotlin有一个lateinit的修饰符在写spring的时候一定要记得用,当然,现在kotlin的坑很多,踩踩玩玩是可以的本文大部分依赖都是从其他博客找到的,侵权立删