IntelliJ IDEA打开多个Maven的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())}
 }
}

yhutils

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)
 }

}

yhapp

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的执行过程++++")

 }

}

yhweb

package com.yh.bi

import java.time.LocalDate

object UserWEB extends Executor{

 override def executor(appkey: String, startDay: LocalDate, endDay: LocalDate): Unit = {

 println("++++我的UserWEB的执行过程++++")

 }

}

yhgame

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Idea配置maven-tomcat-plugin插件实现项目部署

    参考文章: maven tomcat plugin实现热部署:https://www.jb51.net/article/143054.htm 实现maven项目部署到服务器分为如下几个步骤: tomcat 的tomcat-users.xml中添加用户: maven 的settings.xml中添加server: pom.xml中添加tomcat7-maven-plugin插件配置: 在tomcat运行的情况下,运行 tomcat7:deploy 命令. 1.添加用户 在 标签内: <role r

  • idea创建Spring项目的方法步骤(图文)

    Spring介绍 Spring概述 Spring是一个开源框架,Spring是2003年兴起的轻量级java开发框架,由Rod Johnson 在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原形衍生而来.它是为了解决企业级开发的复杂性而创建的.Spring使用基本的javaBaen来完成以前只可能由EJB完成的事情,然而Spring的用途不仅限于服务器端的开发,从简单性.可测试性.低耦合的角度而言任何java应用都可以在s

  • idea远程调试spark的步骤讲解

    spark 远端调试 本地调试远端集群运行的spark项目,当spark项目在集群上报错,但是本地又查不出问题时,最好的方式就是调试一步一步跟踪代码.但是在集群上的代码又不能像本地一样的调试.那么就试试这个调试方法吧. 远程调试spark其实就四步: * 第一步jar包拷贝到集群master节点. * 第二步在 idea 中配置远程机器的IP 和调试端口号. * 第三步:启动远端的spark项目. * 第四步 启动idea 进行调试. 首先 首先了解jvm一些参数属性 -Xdebug -Xrun

  • idea 多模块项目依赖父工程class找不到问题的方法

    比如,我们有这么个过程,项目结构如下: a --b --c a是总结点,b是子节点,c是父节点 b依赖父节点class,通过maven构建时通常我们会在子节点中添加父节点依赖,如: <dependencies> <dependency> <groupId>com.xxx</groupId> <artifactId>c</artifactId> <version>${project.version}</version&

  • idea创建maven项目速度慢的三种解决方案

    困扰 Intellij idea是一款非常强大的编辑器,可以很方便地帮我们创建maven项目,有用过的同学应该都深有体会,但我们经常会遇到一个困扰,那就是用idea创建maven项目时,速度很慢,往往需要好几分钟的时间,有时甚至会卡住,主要原因是创建maven项目时默认是下载求网络上的一个文件archetype-catalog.xml,该文件的大小有5-6M,下载的速度很慢,导致创建过程也变得很慢. 解决办法 解决办法有三种,并且都需要对maven的VM Options参数做配置. 方法一 在m

  • IDEA导入eclipse项目并且部署到tomcat的步骤详解

    前言 本文主要给大家介绍了关于IDEA导入eclipse项目并部署到tomcat的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 步骤如下: 1.首先引入本地项目 我这里是maven项目就直接选择的以maven项目引入,如果选eclipse的话,pom文件不会被初始化,部署tomcat会出问题 这项选完后,就一路next,jdk可以在引入的时候选择,也可以引入后在配置,注意jdk版本要与项目一致 现在项目就从成功引入进来了,可能现在项目会有一大堆红杠,是因为项目还没有配

  • 在IDEA中创建父工程和子模块module的方法步骤

    1.右键选择你所创建的空文件夹,然后new,再点击Module 2.依次选择maven,这里不要点击"create from archetype"点击next 3.填写你想要的GroupId和ArtifactId,然后点击next 4.Module name给父工程起个名字,点击next 5.删除src目录,当然你也可以留着不用删 6.创建一个子模块项目,在父工程上右键点击new Module,依次点击maven,同样不要勾选"create from archetype&qu

  • 详解IDEA下Gradle多模块(项目)的构建

    我们在新起一个项目的时候,一般都会建多个子项目(IDEA里面称之为Module模块).通过Gradle构建,多个Module之间需要将公用的配置抽取到全局,子项目中只写差异化的配置,以便于维护. 多模块项目的Gradle目录结构 示例:我的示例项目demo,我需要有一个common模块用于公用代码,一个rest模块用于提供rest接口,rest依赖common,如果用gradle构建,目录树会是这样: demo ├── build.gradle -- 全局配置 ├── settings.grad

  • Intellij idea下使用不同tomcat编译maven项目的服务器路径方法详解

    问题出现原因: 使用自己下载的tomcat运行maven项目,其中有图片上传模块,图片全部上传到target目录下的工程文件里.结果使用maven的clean插件时,图片全部被删除. 目录如下: 为了解决这个问题,想了如下几种方法: 更改output directory目录 Tomcat 增加虚拟目录.但Intellij idea里的工程运行的是 catalina.sh run 命令,工程外的文件路径访问不到 在第2种方法的基础上,同时运行Tomcat/bin/startup.sh脚本,但比较麻

  • 详解IntelliJ IDEA创建spark项目的两种方式

    Intellij是进行scala开发的一个非常好用的工具,可以非常轻松查看scala源码,当然用它来开发Java也是很爽的,之前一直在用scala ide和eclipse,现在换成intellij简直好用到飞起,但是有些人不知道怎么用intellij去创建一个spark项目,这里介绍两种 1.选择File->new Project->Java->Scala,这里scala版本是2.11.8 2 .之后一路点击next,直到finish,创建完的项目见下图,这时候已经可以创建scala文件

随机推荐