1. 程式人生 > >專案重構00 Java maven專案下,整合Scala混合開發

專案重構00 Java maven專案下,整合Scala混合開發

作為補充我找了個Scala專案,但這不是我重構的了,旨在說明問題,沒什麼區別。

pom檔案,這個是我的專案的pom,其他依賴可以不用寫。

github 專案地址 https://github.com/jxnu-liguobin/SpringBoot-Base-System

注意Scala版本號

注意Scala版本號

注意Scala版本號

<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>cn.jxnu.edu</groupId>
	<artifactId>base</artifactId>
	<version>1.0-SNAPSHOT</version>

	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
	</parent>

	<properties>
		<!--跳過測試 -->
		<skipTests>true</skipTests>
		<shiro.version>1.2.4</shiro.version>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<!-- scala 這裡2個版本的號必須對應,否則編譯失敗,自己仔細看倉庫的號 -->
		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-library</artifactId>
		<version>2.11.8</version>
		</dependency>
		<dependency>
			<groupId>com.typesafe.scala-logging</groupId>
			<artifactId>scala-logging-slf4j_2.11</artifactId>
			<version>2.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot熱部署依賴 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- 新增freemarker資源 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
		<!--新增spring boot 配置處理器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- 新增SpringData JPA -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<!-- 新增SpringBoot快取依賴 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<!-- 單元測試 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--新增資料庫連線池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.15</version>
		</dependency>
		<!-- Ehcache -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
		</dependency>
		<!--新增Shiro -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>${shiro.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>${shiro.version}</version>
		</dependency>
		<!-- Shiro 標籤 -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-web</artifactId>
			<version>${shiro.version}</version>
		</dependency>
		<!--新增資料庫驅動 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!--新增工具 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.46</version>
		</dependency>
		<!-- 新增日期/時間庫 -->
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
		</dependency>

		<dependency>
			<groupId>org.jadira.usertype</groupId>
			<artifactId>usertype.core</artifactId>
			<version>5.0.0.GA</version>
		</dependency>

		<dependency>
			<groupId>org.jadira.usertype</groupId>
			<artifactId>usertype.spi</artifactId>
			<version>5.0.0.GA</version>
		</dependency>
		<!-- 新增aop支援 CGLIB需要配置spring.aop.proxy-target-class=true,不然預設使用的是標準Java的實現。 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<!-- 新增redis快取支援 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-ehcache</artifactId>
			<version>1.2.4</version>
		</dependency>
		<!-- 減少冗餘 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>

	<build>
<!-- 指定Scala的目錄,我採用maven預設目錄,你也可以指定為src/main/scala,但是你的原始檔也應該是這個-->
		<sourceDirectory>src/main/java</sourceDirectory>
		<testSourceDirectory>src/test/java</testSourceDirectory>
		<plugins>
			<plugin><!-- Scala編譯外掛 -->
				<groupId>org.scala-tools</groupId>
				<artifactId>maven-scala-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>testCompile</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<recompileMode>incremental</recompileMode>
					<scalaVersion>2.11.8</scalaVersion>
					<launchers><!-- 啟動類,是java或者scala都可以,全路徑名!!!! -->
						<launcher>
							<id>app</id>
							<mainClass>cn.edu.jxnu.base.Application</mainClass>
							<args>
								<arg>-deprecation</arg>
							</args><!-- 啟動虛擬機器引數-->
							<jvmArgs>
								<jvmArg>-Xms64m</jvmArg>
								<jvmArg>-Xmx1024m</jvmArg>
							</jvmArgs>
						</launcher>
					</launchers>
				</configuration>
			</plugin>
			<plugin><!-- maven自帶編譯外掛,編譯java-->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

			<!-- 在Goals中輸入:org.apache.maven.plugins:maven-jar-plugin:2.4:jar org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>cn.edu.jxnu.base.Application</mainClass>
					<fork>true</fork> <!-- 如果沒有該配置,devtools不會生效 -->
				</configuration>
				<dependencies>
					<!-- spring熱部署 -->
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>springloaded</artifactId>
						<version>1.2.5.RELEASE</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

這裡有幾點注意是,如果使用Lombok外掛,自動編譯生產get/set方法,在寫Scala程式碼的時候,可能是找不到set/get,此時需要重新編譯Scala【我是隨便找個scala專案演示】

選擇restart Presentation complier 即可


最好設定一下eclipse編譯順序,先編譯Java,保證scala編譯之前已經有Java的set/get方法,這樣即使有打叉,但是執行是OK的。


增加maven配置後還是有Scala程式碼有大紅色的叉叉,就試著給專案新增Scala本地庫依賴


IDEA 類似。只是選單位置不同。

僅供參考。