IntelliJ IDEA開啟多個Maven的module且相互呼叫程式碼
##目錄
###1、需求
###2、注意事項(6點非常重要)
###3、建立Project和建立module
###4、專案之前的依賴關係
###5、程式碼展示
###6、專案中POM依賴展示
###7、執行結果展示
###8、如果建立Java 的module
###1、需求
1、IntelliJ IDEA開啟多個專案
2、每個同學開發一個專案,相互之前獨立不影響
3、通過一個入口可以呼叫所有專案類、方法、屬性,達到同時開發且檢測程式碼
4、dependency只需要寫一份,其餘專案不用寫,便可全部依賴
###2、注意事項(非常重要)
6個坑:
1、<groupId>com.yh.bi</groupId>
專案中所有的groupId要一樣
2、避免迴圈依賴,導致程式報錯
3、<scope>provided</scope>
打包的伺服器執行時候需要provided,本機除錯的時候,需要註釋
在一個maven專案中,如果存在編譯需要而釋出不需要的jar包,可以用scope標籤,值設為provided
4、專案、module建好之後需要新增Scala的框架支援
5、在yhProject中,可以統一對所有的module進行清理、編譯、打包
6、要執行依賴中的module,則必須要將module中的Jar包,打到maven中,需要使用install
下面,是我將所有module中的Jar打到Maven中的路徑:
###3、建立Project和建立module
1、只需要建立一個專案,其他專案由module建立,所有module且放在專案中。
2、本文專案為yhproject,其餘都為module,分別是:mainEntrance、yhutils、yhapp、yhweb、yhgame
專案建立步鄹:
Module建立步鄹:
專案、所有module、部分module程式碼展示:
###4、專案之前的依賴關係
###5、程式碼展示
mainEntrance
package com.yh.bi.dag
package com.yh.bi.dag /** * Created by yuhui on 2017/2/10. */ import java.time.{Duration, LocalDate} import com.yh.bi._ import com.yh.bi.{UserAPP, UserGame, UserWEB} import org.slf4j.LoggerFactory import scala.collection.immutable.{ListMap, ListSet} /** * Created by yuhui on 2016/8/25. * task --> Node --> DAG --> DAGExecutor */ case class Node[T](task: T, parent: T*) { override def toString: String = { s"$task(${parent.mkString(",")})" } } case class DAG[T](nodes: Node[T]*) case class DAGExecutor[T](dag: DAG[T]) { private val LOG = LoggerFactory.getLogger(this.getClass) private val _nodes: Map[T, Seq[T]] = dag.nodes.map(node => (node.task, node.parent.filter(_ != null))).toMap private var _pending: Set[T] = ListSet() private var _fails = ListMap[T, String]() private var _success = Seq[T]() //判斷Node的task節點的父節點執行狀態(flase ,true) private def getPending: Option[T] = { _pending.find { name => val parents = _nodes(name) !parents.exists(name => !_success.contains(name)) } } private def fail(name: T, message: String): Unit = { _pending -= name _fails += name -> message for (child <- _pending.filter(child => _nodes(child).contains(name))) { fail(child, s"依賴的任務無法執行: $name") } } private def success(name: T): Unit = { _pending -= name _success = _success :+ name } def execute(func: T => Unit): Unit = { _pending = _nodes.keySet _fails = ListMap() _success = Seq() var running = true while (running) { val taskOpt = getPending if (taskOpt.nonEmpty) { val task = taskOpt.get val startMills = System.currentTimeMillis() LOG.info("start task {}", task) try { println("=============") func(task) //執行executor方法 println("+++++++++++++") val time = Duration.ofMillis(System.currentTimeMillis() - startMills) LOG.info(s"end task $task time=$time") success(task) } catch { case e: Throwable => fail(task, e.getMessage) LOG.error(e.getMessage, e) LOG.info(s"fail task $task") } } else { running = false } } for (name <- _success) { LOG.info(s"success task: $name") } for (name <- _fails) { LOG.info(s"fail task: ${name._1} - ${name._2}") } } } object DAG { val allSDKDAG = new DAG[Task]( Node(UserAPP), Node(UserGame), Node(UserWEB) ) def main(args: Array[String]): Unit = { DAGExecutor(allSDKDAG).execute { task =>task.executor("appkey": String, LocalDate.now(), LocalDate.now())} } }
package com.yh.bi
/**
* Created by yuhui on 2017/2/10.
*/
import java.time.LocalDate
import org.apache.spark.sql.SQLContext
import org.slf4j.LoggerFactory
abstract class Executor extends Task with SQLContextAware {
override def run(appkey: String, startDay: LocalDate, endDay: LocalDate)={}
}
trait SQLContextAware {
implicit var ctx: SQLContext = _
}
abstract class Task {
protected val LOG = LoggerFactory.getLogger(this.getClass)
def executor(appkey: String, startDay: LocalDate, endDay: LocalDate): Unit
def run(appkey: String, startDay: LocalDate, endDay: LocalDate): Unit = {
executor(appkey, startDay, endDay)
}
}
package com.yh.bi
/**
* Created by yuhui on 2017/2/10.
*/
import java.time.LocalDate
object UserAPP extends Executor{
override def executor(appkey: String, startDay: LocalDate, endDay: LocalDate): Unit = {
println("++++我的UserAPP的執行過程++++")
}
}
package com.yh.bi
import java.time.LocalDate
object UserWEB extends Executor{
override def executor(appkey: String, startDay: LocalDate, endDay: LocalDate): Unit = {
println("++++我的UserWEB的執行過程++++")
}
}
package com.yh.bi
/**
* Created by yuhui on 2017/2/10.
*/
import java.time.LocalDate
object UserGame extends Executor{
override def executor(appkey: String, startDay: LocalDate, endDay: LocalDate): Unit = {
println("++++我的UserGame的執行過程++++")
}
}
###6、專案中POM依賴展示
yhproject中POM檔案展示:
<?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.yh.bi</groupId>
<artifactId>yhproject</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>mainEntrance</module>
<module>yhapp</module>
<module>yhweb</module>
<module>yhgame</module>
<module>yhutils</module>
</modules>
</project>
mainEntrance中POM檔案展示:
<?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>
<artifactId>mainEntrance</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope> //本機除錯則註釋, 叢集執行則解開-->
</dependency>
<dependency>
<artifactId>yhapp</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<artifactId>yhgame</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<artifactId>yhweb</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project>
yhutils中POM檔案展示:
<?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">
<parent>
<artifactId>yhproject</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yhutils</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project>
yhapp中POM檔案展示:
<?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.yh.bi</groupId>
<artifactId>yhapp</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project>
yhweb中POM檔案展示:
<?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.yh.bi</groupId>
<artifactId>yhweb</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project>
yhgame中POM檔案展示:
<?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.yh.bi</groupId>
<artifactId>yhgame</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project>
###7、執行結果展示
注意:我在mainEntrance執行DAG中的main檔案,可以呼叫且執行了yhutils、yhapp、yhweb、yhgame中的程式碼
###8、如果建立Java 的module