搭建 Ice 开发环境

可以使用 Eclipse + Maven 以及 IDEA + Gradle 的方式来开发 Ice 项目,建议使用后者。

选择 Ice 版本

Ice 版本及相关软件对应关系如下表所示——

版本 JDK 下载 构建工具 IDE
3.7 1.8 Windows 版本 Gradle IDEA
3.6 1.7, 1.8 Windows 版本 Gradle IDEA
3.5 1.6, 1.7 Windows 版本 Maven Eclipse

Ice Linux 版本下载方式详见官网:https://zeroc.com/distributions/ice

本文以 Ice 3.6.3 版本为例。

Ice 下载、安装完成后,将其安装路径添加到名为 “ICE_HOME” 的环境变量中,同时添加 “%ICE_HOME%\bin” 到 Path 环境变量中,确保其中的命令行可以在命令行界面或 IDE 中执行。

上述安装和设置完成后,打开 Windows 命令行,执行 slice2java -v,如果出现版本号,如 “3.6.3”,则表示 Ice 安装成功,可以正常使用了。

搭建开发环境

使用 Eclipse 开发 Ice 项目

使用 Eclipse 开发 Ice 项目,适合 Ice 3.6 以下版本(如 Ice 3.5)。相关软件为——

  • JDK,如 jdk1.7.0_80
  • Eclipse,如 Eclipse Neon
  • Ice for Eclipse 插件

Ice for Eclipse 插件其实是做了一个 Ant Task,负责把 Slice 文件自动转换为 Java 类。在 Eclipse 中修改了 Slice 文件并保存时,这个插件会自动调用 slice2java.exe 命令生成 Java 源码。

Eclipse Help → Install New Software,然后输入 http://www.zeroc.com/download/eclipse,并在出现的结果列表中选择相应的 Ice for Eclipse 项目,安装即可。

插件安装完成后,需在 Eclipse 中配置 Ice 的安装路径。在 Eclipse 菜单 Window → Preferences 中选择 Ice Builder,然后在右边的 SDK Location 输入框中输入 Ice 的安装路径,如 “C:\Program Files (x86)\ZeroC\Ice-3.6.3”。

插件安装和设置完成后,我们新建一个 Java 工程 icetest,然后在工程上点击右键,找到 “Ice Builder” 菜单,点击 “Add Ice Builder”。

我们再看一下项目结构,会发现新增了两个文件夹。一个是 slice 文件夹,这个是用来存放 Slice 文件的地方。另外一个 generated 文件夹,存放的是根据 Slice 文件自动转换生成的 Java 源码,项目编译运行需要用到它们。

Eclipse 配合 Maven,构建 Ice 项目

公司的项目绝大多数都已经采用了 Maven 管理和编译,那么怎么用 Maven 来标准化 Ice 项目呢?这里面主要涉及到两个问题:

  • Slice 文件在 Maven 里的编译问题
  • 自动生成的 Ice 服务的代码的位置问题

为了解决 Slice 的编译问题,Ice 提供了一个 Ant 任务 slice2java,依赖于 Ice 中的 ant-ice.jar(注意:此 jar 包在 Ice 3.6 及以上版本中不再提供,也即 Ice 3.6 及 Ice 3.7 只支持 Gradle 构建):

<taskdef name="slice2java" classname="Slice2JavaTask" classpathref="maven.plugin.classpath" />

其用法如下,指定编译哪些文件(includes),以及输出 Java 源文件到哪个目录(outputdir):

<slice2java outputdir="generated">
    <fileset dir="slice" includes="*.ice"></fileset>
</slice2java>

我们在标准的 POM 文件中增加 Slice 文件的编译任务,并且增加将编译后的源码加入 Source 的任务,即可解决上面的两个问题。

完整的 pom.xml 文件如下:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.ucmed</groupId>
    <artifactId>icetest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <slice.home.dir>C:\Program Files (x86)\ZeroC\Ice-3.5.1</slice.home.dir>
    </properties>

    <repositories>
        <!-- 如有 Nexus 私服, 请添加 -->

        <repository>
            <id>ice</id>
            <name>ice Nexus Repository</name>
            <url>https://repo.zeroc.com/nexus/content/repositories/releases/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.zeroc</groupId>
            <artifactId>ice</artifactId>
            <version>3.5.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="slice2java">
                                <taskdef name="slice2java" classname="Slice2JavaTask" classpathref="maven.plugin.classpath" />
                                <slice2java outputdir="generated">
                                    <fileset dir="slice" includes="*.ice"></fileset>
                                </slice2java>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.zeroc</groupId>
                        <artifactId>ant-ice</artifactId>
                        <scope>system</scope>
                        <systemPath>${slice.home.dir}\lib\ant-ice.jar</systemPath>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>generated</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

使用 IDEA 开发 Ice 项目

使用 IDEA 开发 Ice 项目,适合 Ice 3.6 及以上版本(如 Ice 3.6.3)相关软件为——

  • JDK,如 jdk1.8.0_111
  • IntelliJ IDEA,如 IDEA 2017.2

IDEA 配合 Gradle,构建 Ice 项目

关于 Gradle 的使用,这里不过多介绍,下面来看一个写好的 build.gradle 文件吧。

// 项目标识
group 'com.ucmed.ice.study'
version '1.0-SNAPSHOT'

// 使用 java 插件
apply plugin: 'java'

// 源码兼容级别
sourceCompatibility = 1.8

// 仓库
repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url 'http://192.168.0.114:8081/nexus/content/repositories/releases'
    }
    maven {
        url 'https://repo.zeroc.com/nexus/content/repositories/releases'
    }
}

// 项目依赖
dependencies {
    compile 'com.zeroc:ice:3.6.3'
    compile 'com.zeroc:icebox:3.6.3'
    compile 'org.slf4j:slf4j-api:1.7.9'
    compile 'ch.qos.logback:logback-classic:1.2.3'
    compile 'ch.qos.logback:logback-core:1.2.3'
    compile 'org.springframework:spring-context:4.2.5.RELEASE'
    compile 'org.springframework:spring-beans:4.2.5.RELEASE'
    compile 'org.springframework:spring-core:4.2.5.RELEASE'
    compile 'org.springframework:spring-web:4.2.5.RELEASE'
    compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'
    compile 'org.springframework:spring-aop:4.2.5.RELEASE'
    compile 'org.springframework:spring-context-support:4.2.5.RELEASE'
    compile 'org.springframework:spring-tx:4.2.5.RELEASE'
    compile 'org.springframework:spring-orm:4.2.5.RELEASE'
    compile 'org.springframework:spring-jdbc:4.2.5.RELEASE'
    compile 'org.springframework:spring-expression:4.2.5.RELEASE'
    compile 'org.hibernate:hibernate-core:5.2.10.Final'
    compile 'org.apache.commons:commons-dbcp2:2.1.1'
    compile 'mysql:mysql-connector-java:6.0.6'
    testCompile 'junit:junit:4.12'
}

// compileSlice 任务,编译 Slice 文件,生成 Java 源码
task compileSlice(type: Exec) {

//    commandLine 'slice2java', '--output-dir', "${projectDir}/src/main/java",
//            "${projectDir}/slice/TicketServer.ice"

    // output-dir 此目录存放自动生成的 Java 源码
    // underscore 允许 Slice 标识符带有下划线
    commandLine 'slice2java', '--underscore', '--output-dir', "${projectDir}/src/main/java",
            "${projectDir}/slice/DoctorServer.ice"

    // 后续如果有其他 Slice 文件需要编译成 Java 文件,就按照上面的格式,在这里添加即可
    // 记得添加完成后,点击 IDEA 右边的 Gradle 工具条,手动运行 compileSlice 任务,或直接点击 assemble/build 构建

    standardOutput = new ByteArrayOutputStream()

    ext.output = {
        println standardOutput.toString()
    }

}

// copyJars 任务,是我们自定义的任务,它将项目依赖的 jar 包拷贝到项目根目录下的 lib 文件夹,以供 IceGrid 使用 
task copyJars(type: Copy) {
    from configurations.runtime
    into 'lib'
}

// 设置 UTF-8 编码,以免在构建时出现中文乱码
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

// compileJava 任务依赖 compileSlice 任务   
gradle.projectsEvaluated {
    compileJava.dependsOn(compileSlice)
} 

Ice 官方也提供了一个叫做 slice 的 Ice Builder for Gradle 插件,对应的 build.gradle 文件示例如下:

// 项目标识(根据实际进行替换)
group 'com.ucmed.ice'
version '1.0-SNAPSHOT'

// 使用 java 插件
apply plugin: 'java'

// 源码兼容级别(根据实际进行替换)
sourceCompatibility = 1.8

// 项目依赖的 jar 包所在仓库
repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url "http://192.168.0.114:8081/nexus/content/repositories/releases"
    }
    maven {
        url "https://repo.zeroc.com/nexus/content/repositories/releases"
    }
}

// 项目依赖
dependencies {
    compile group: 'com.zeroc', name: 'ice', version: '3.6.3'
    compile group: 'com.zeroc', name: 'icebox', version: '3.6.3'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

// 项目构建脚本
buildscript {
    // 脚本依赖的 jar 包所在仓库
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    // 脚本依赖
    dependencies {
        classpath "gradle.plugin.com.zeroc.gradle.ice-builder:slice:1.4.1"
    }
}

// 使用 slice 插件
apply plugin: "com.zeroc.gradle.ice-builder.slice"

// slice 属性定义,更多可参考 https://github.com/zeroc-ice/ice-builder-gradle
slice {
    // 定义自动生成的代码存储目录,Slice 文件默认目录为 /src/main/slice
    output file("/src/main/java")
}

// 设置 UTF-8 编码,以免在构建时出现中文乱码
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

相比而言,第二个 build.gradle 文件更为简洁,我们不需要像第一个文件那样,为每一个 Slice 文件写一行编译说明了。所以,推荐大家参考第二个文件,来写我们实际项目的 build
.gradle
文件。

总结

建议使用 Gradle 构建 Ice 项目,以使用 Ice 的新发布版本,体验最新的特性和及时的 Bug 修复。可以在上面这个 build.gradle 文件的基础上进行相应改动,以满足实际项目需要。

开发时,可以在 IDEA 中配置本地安装好的 gradle 版本。Ice 官方使用的 gradlew 要下载全部版本的 gradle ,由于众所周知的网络原因,下载时基本都会卡住不动,所以不推荐。

文章目录
  1. 1. 选择 Ice 版本
  2. 2. 搭建开发环境
    1. 2.1. 使用 Eclipse 开发 Ice 项目
      1. 2.1.1. Eclipse 配合 Maven,构建 Ice 项目
    2. 2.2. 使用 IDEA 开发 Ice 项目
      1. 2.2.1. IDEA 配合 Gradle,构建 Ice 项目
  3. 3. 总结
|