1. 程式人生 > >搭建eclipse+maven+scala-ide的scala web開發環境

搭建eclipse+maven+scala-ide的scala web開發環境

http://www.tuicool.com/articles/NBzAzy

江湖傳聞,scala開發的最佳利器乃 JetBrains 的神作 IntelliJ IDEA ,外加構建工具sbt 是也。

但因歷史原因,專案組成員對 Eclipse + Maven 組合更為熟悉,為了快速實現專案原型,不增加不確定因素帶來的風險,搭建一套 Eclipse + Maven + Scala-IDE 的開發環境。

基本原則是,必須完全滿足但不限於以下幾點內容:

  • 方便重構,方便除錯,支援熱部署。
  • 可直接使用已有maven的本地和私服倉庫。
  • 可以無束縛的只用自己熟悉的語言編寫程式碼。
  • 可以快速混合編譯scala+java程式碼,包括交叉引用的檔案。

如果你有潔癖,可以自己下載 Eclipse ,然後安裝各種外掛。但是可能會遇到外掛依賴包版本衝突之類的問題,為了速度,我直接下載官方打包好的 Scala-IDE ,有各種平臺可供選擇。

使用 Git 管理專案原始碼,需要安裝 EGit 外掛,Eclipse外掛更新地址 EGit Updates 。

假設專案名稱為 feeling ,使用 JDK 1.7,Servlet 3.0,最終目錄結構如下。

.
├── .settings                    #eclipse工程目錄
├── .classpath                   #eclipse classpath檔案
├── .project
#eclipse project檔案 ├── src #原始碼 | ├── main #原始碼主目錄 | | ├── java #java程式碼 | | ├── scala #scala程式碼 | | ├── resources #資原始檔 | | └── webapp #web主目錄 | | ├── WEB-INF #WEB-INF目錄
| | | └── web.xml #web.xml檔案 | | └── index.jsp #主頁面 | └── test #測試程式碼 | ├── java #java測試程式碼 | ├── scala #scala測試程式碼 | └── resources #測試資原始檔 ├── .gitignore #git忽略配置 ├── target #編譯輸出目錄 ├── README.md #markdown格式的說明檔案 └── pom.xml #maven的pom檔案

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>feeling</groupId>
  <artifactId>feeling</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>

  <!-- <name>${project.artifactId}</name> -->
  <name>feeling</name>
  <description>our wonderfully feeling application</description>
  <url>http://feeling.com</url>
  <inceptionYear>2014</inceptionYear>

  <organization>
    <name>feeling</name>
    <url>http://feeling.com</url>
  </organization>

  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <developers>
    <developer>
      <id>bruce</id>
      <name>bruce sha</name>
      <url>http://bruce-sha.github.io</url>
      <email>[email protected]</email>
    </developer>
  </developers>

  <scm>
  	<connection>http://17.20.13.23/scm/git/feeling</connection>
  	<developerConnection>http://17.20.13.23/scm/git/feeling</developerConnection>
  	<url>http://17.20.13.23</url>
  </scm>

  <properties>
    <scala.version>2.10.3</scala.version>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <encoding>UTF-8</encoding>
  </properties>

  <!-- 個性化開發 -->
  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <build.param>this is dev</build.param>
      </properties>
    </profile>
    <profile>
      <id>release</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <properties>
        <build.param>this is relase</build.param>
      </properties>
    </profile>
  </profiles>

  <dependencies>

    <!-- google -->
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>15.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>3.0</version>
    </dependency>

    <!-- servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <!-- <version>2.5</version> -->
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>

    <!-- <dependency> -->
    <!-- <groupId>javax.servlet</groupId> -->
    <!-- <artifactId>jsp-api</artifactId> -->
    <!-- <version>2.0</version> -->
    <!-- <scope>provided</scope> -->
    <!-- </dependency> -->

    <!-- scala -->
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>

    <!-- test -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 其他包不再一一描述 -->    
    <!-- log -->
    <!-- json -->
    <!-- mongodb -->
    <!-- quartz -->

  </dependencies>

  <build>
    <finalName>feeling</finalName>

    <!-- 必須要,資原始檔中佔位符被profile替換的關鍵配置 -->
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>*.*</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>

    <!-- 必須幹掉,否則不編譯src/main/java下的程式碼 -->
    <!-- <sourceDirectory>src/main/scala</sourceDirectory> -->
    <!-- <testSourceDirectory>src/test/scala</testSourceDirectory> -->
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.1.6</version>
        <!-- 必須要,否則不能混合編譯交叉引用檔案 -->
        <executions>
          <execution>
            <id>scala-compile-first</id>
            <phase>process-resources</phase>
            <goals>
              <goal>add-source</goal>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>scala-test-compile</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.13</version>
        <configuration>
          <useFile>false</useFile>
          <disableXmlReport>true</disableXmlReport>
          <!-- If you have classpath issue like NoDefClassError,... -->
          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
          <includes>
            <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
          </includes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <!-- 移除web.xml的依賴,Servlet 3.0可以不要web.xml檔案 -->
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>

      <!-- jetty6,不支援servlet3 -->
      <!-- <plugin> -->
      <!-- <groupId>org.mortbay.jetty</groupId> -->
      <!-- <artifactId>maven-jetty-plugin</artifactId> -->
      <!-- <version>6.1.26</version> -->
      <!-- <configuration> -->
      <!-- <scanIntervalSeconds>10</scanIntervalSeconds> -->
      <!-- <stopKey>foo</stopKey> -->
      <!-- <stopPort>9999</stopPort> -->
      <!-- </configuration> -->
      <!-- <executions> -->
      <!-- <execution> -->
      <!-- <id>start-jetty</id> -->
      <!-- <phase>pre-integration-test</phase> -->
      <!-- <goals> -->
      <!-- <goal>run</goal> -->
      <!-- </goals> -->
      <!-- <configuration> -->
      <!-- <scanIntervalSeconds>0</scanIntervalSeconds> -->
      <!-- <daemon>true</daemon> -->
      <!-- </configuration> -->
      <!-- </execution> -->
      <!-- <execution> -->
      <!-- <id>stop-jetty</id> -->
      <!-- <phase>post-integration-test</phase> -->
      <!-- <goals> -->
      <!-- <goal>stop</goal> -->
      <!-- </goals> -->
      <!-- </execution> -->
      <!-- </executions> -->
      <!-- </plugin> -->

      <!-- tomcat7:run 注意tomcat:run跑的是6,不支援servlet3 -->
      <plugin>
        <!-- http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin -->
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/</path>
          <port>80</port>
        </configuration>
      </plugin>

      <!-- jetty:run -->
      <plugin>
        <!-- http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin -->
        <groupId>org.mortbay.jetty</groupId>
        <!-- <artifactId>maven-jetty-plugin</artifactId> 這是jetty6 不支援servlet3 -->
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.13.v20130916</version>
        <configuration>
          <stopPort>9966</stopPort>
          <stopKey>foo</stopKey>
          <scanIntervalSeconds>0</scanIntervalSeconds>
          <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
              <port>80</port>
              <maxIdleTime>60000</maxIdleTime>
            </connector>
          </connectors>
          <webAppConfig>
            <contextPath>/</contextPath>
          </webAppConfig>
        </configuration>
      </plugin>

    </plugins>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- <web-app -->
<!-- xmlns="http://java.sun.com/xml/ns/javaee" -->
<!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" -->
<!-- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" -->
<!-- version="2.5" -->
<!-- > -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">

  <display-name>feeling</display-name>

  <!--   <servlet> -->
  <!--   <servlet-name>feeling</servlet-name> -->
  <!--   <servlet-class>feelings.service.FeelingService</servlet-class> -->
  <!--   </servlet> -->

  <!--   <servlet-mapping> -->
  <!--   <servlet-name>feeling</servlet-name> -->
  <!--   <url-pattern>/feeling</url-pattern> -->
  <!--   </servlet-mapping> -->
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

.project檔案:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
  <name>feeling</name>
  <comment></comment>
  <projects>
  </projects>
  <buildSpec>
    <buildCommand>
      <name>org.scala-ide.sdt.core.scalabuilder</name>
      <arguments>
      </arguments>
    </buildCommand>
    <buildCommand>
      <