Overview
接下来所有代码都会放在同一个 Maven 工程中,所以以下先创建一个 Maven 项目。
建立工程
首先打开 IDEA,选择 File
-> New
-> Project
在弹出的对话框左侧选择 Maven
,然后选择 Next
,进入下一个界面。
接着在 GroupId
一栏填入 com.bookislife
,在 ArtifactId
一栏填入 jgsk
,继续 Next
Project name
一栏填入 JGSK
,Project location
为项目存放地址,随便填,最后点击 Finish
完成工程的创建。
然后在工程的根目录下执行以下命令创建各目录
➜ JGSK git:(master) ✗ mkdir -p src/main/java/com/bookislife/jgsk/java
➜ JGSK git:(master) ✗ mkdir -p src/main/groovy/com/bookislife/jgsk/groovy
➜ JGSK git:(master) ✗ mkdir -p src/main/scala/com/bookislife/jgsk/scala
➜ JGSK git:(master) ✗ mkdir -p src/main/kotlin/com/bookislife/jgsk/kotlin
➜ JGSK git:(master) ✗ mkdir -p src/test/java/com/bookislife/jgsk/java
➜ JGSK git:(master) ✗ mkdir -p src/test/groovy/com/bookislife/jgsk/groovy
➜ JGSK git:(master) ✗ mkdir -p src/test/scala/com/bookislife/jgsk/scala
➜ JGSK git:(master) ✗ mkdir -p src/test/kotlin/com/bookislife/jgsk/kotlin
安装 IDEA 插件
在编写程序前,还需要为 IDEA 安装几个插件,安装步骤如下
选择 Settings (Mac 平台为 Preferences)
- Plugins
- Browse repositories
,在弹出的对话框中的输入框中输入以下几个插件并安装,安装完毕后重启 IDEA
需要安装的插件有以下几个
- Scala
- GMavenPlus IntelliJ Plugin
- Groovy
- Kotlin
修改 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bookislife</groupId>
<artifactId>jgsk</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.encoding>UTF-8</project.build.encoding>
<jdk.version>1.8</jdk.version>
<maven-compiler-plugin.verion>3.2</maven-compiler-plugin.verion>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<groovy.version>2.4.9</groovy.version>
<gmavenplus-plugin.version>1.5</gmavenplus-plugin.version>
<scala.version>2.12.1</scala.version>
<scala-maven-plugin.version>3.2.0</scala-maven-plugin.version>
<kotlin.version>1.1.0</kotlin.version>
</properties>
<repositories>
<repository>
<id>sonatype.oss.snapshots</id>
<name>Sonatype OSS Snapshot Repository</name>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sonatype.oss.snapshots</id>
<name>Sonatype OSS Snapshot Repository</name>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.verion}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<sources>
<source>
<directory>src/main/groovy</directory>
</source>
</sources>
<testSources>
<source>
<directory>src/test/groovy</directory>
</source>
</testSources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
修改完 pom.xml
后进行保存。接着选择该 pom.xml
文件,单击右键选择 Maven
- Reimport
,执行完成后左边工程的 main
下的目录文件夹名应该除了 kotlin
之外都变成了蓝色,test
下除了 kotlin
之外都变成了绿色。
为了使 IDEA 也认出 kotlin
文件夹,需要进行以下操作:
选择 File
- Project Structure...
,在弹出的对话框中选择 Modules
条目,选择 main/kotlin
目录并点击上方蓝色的 Sources
按钮,接着选择 test/kotlin
目录并点击上方绿色的 Tests
按钮,点击 OK
关闭对话框。
此时,整个工程结构就创建完成了。
整个项目源码可以见 https://github.com/SidneyXu/JGSK
网友评论