详情可参考:
Gradle构建多模块项目
选择本地的gradle,保证gradle的版本至少为4或以上。
填写group、artifactid、版本号。
然后,创建多个模块,假设背景为需要进行远程调用的提供者服务,这里以 api 和 web 模块为例,
创建成功。
修改根项目下面的build.gradle
例子:
/***所有项目共通***/
group 'net.w2p'
version '1.0-SNAPSHOT'
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
sourceCompatibility = 1.8
targetCompatibility = 1.8
idea {
module {
inheritOutputDirs = true
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(GroovyCompile) {
groovyOptions.encoding = "MacRoman"
}
// java编译的时候缺省状态下会因为中文字符而失败
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
repositories {
maven{
//更换为阿里的仓库
url 'http://maven.aliyun.com/nexus/content/groups/public'
}
//有些jar包在中央仓库是没有的,需要手动添加上去
// flatDir { dirs 'local_jars' }
// mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
// 所有子项目的通用配置
subprojects {
dependencies {
// 通用依赖
// 测试依赖
testCompile(
"junit:junit:4.12"
)
}
}
在web项目下面引入api项目的依赖,
plugins {
id 'java'
id 'war'
}
group 'net.w2p'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile project(":FileServerApi")
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
然后,编译web项目部署到tomcat下面,tomcat部署设置如下:
FileServerApi项目正常编译过来,而且FileServerWeb项目的资源以及jsp文件正常部署。
假如到上面一步你可以直接访问到index.jsp,那么就不用往下面看了。
假如不是的话,而是
这样无法访问,可以继续往下面看。
当看到这个的时候查了一下资料,有:
IntelliJ IDEA新建Gradle项目启动404
另外一篇文章,
IDEA Gradle 项目 Tomcat运行 404
作者说的是:
以下图片文字都摘抄自【小孩小烦恼】的博客,请知悉。
【小孩小烦恼】的文章抄录完毕
上面的方法是针对其中一部分问题的解决方案,不过,话说,Store generated project files externally 到底是什么,为什么会导致404的?下面进行求证
我们分别对setting里面的Store generated project files externally进行留空以及勾选,然后查看一下生成的文件目录有无变化,以此求证。
额,上面jsp文件没有,jar编译后的lib也没有,都为空-
这次算是正常了,所以结论是:
Store generated project files externally 这个选项的意思是,是否将编译好的文件到输出到目标目录下面。
一般情况下,我们配置好的tomcat的网站目录都是
这样的目录,一旦设置不输出,那么默认情况下是不可能能够正常运行网站的。
虽然Store generated project files externally不是引起404的原因,但是受到启发,查看一下部署的设置,结果发现了:
看到没有,application context—网站上下文简直乱码一样,于是,改为:
就是网站上下文context path的问题了。
gradle多模块多项目的创建实际上只是一个基础,以后将用来进行分布式服务提供者程序的开发。
下面放出基本demo例子供下载:
【gradle】idea+gradle 多模块项目创建教程入门例子资源
根据上面的步骤,固然可以新建一个多模块项目,但是总感觉就是一堆代码堆砌而成,完全没有整体的概念,针对这个问题,这里做了一下优化,赋予根目录更多责任和权限。
现在规定根项目负责各个子模块的版本管理以及依赖,根目录的build.gradle内容变更为:
plugins {
id 'java'
}
group 'net.w2p'
version '1.0-SNAPSHOT'
/***所有项目共通***/
allprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
ext{
/***常见或主要第三方依赖版本号定义 begin***/
globalSpringVersion = "5.1.4.RELEASE"
globalSpringDataJpaVersion ="2.1.2.RELEASE"
globalSpringBootVersion = '2.1.1.RELEASE'
globalFastJsonVersion="1.2.54"
globalMyBatisVersion="3.4.6"
globalGoogleGuavaVersion="27.0.1-jre"
globalDom4jVersion="1.6.1"
globalJavaMailVersion="1.4.7"
globalJsoupVersion="1.11.3" //--一个过滤html危险字符串api,用于web安全
globalQuartzVersion="2.3.0"
globalFlexmarkVersion="0.34.32" //--java对markdown语法的解释以及翻译api
globalPostgresqlJdbcDriverVersion="42.2.5"
globalQiniuSdkVersion="7.2.18"//--七牛上传下载客户端sdk
globalApacheAntVersion="1.10.5"
globalGoogleZXingVersion="3.3.3"
globalFastdfsClientVersion="1.27"
globalLog4jVersion="1.2.17"
globalSlf4jVersion="1.7.25"
globalRedisClientVersion="2.10.1"
globalFreemarkerVersion="2.3.28"
/***常见或主要第三方依赖版本号定义 end***/
/****常见或者程序主要引用依赖定义 begin****/
//--这个是spring boot要直接compile进去的框架。
ref4SpringBoot=[
/***spring boot 相关依赖***/
"org.springframework.boot:spring-boot:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter-web:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter-freemarker:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-devtools:$globalSpringBootVersion"
]
//--这个是spring boot要compileOnly的类库
ref4SpringBootProvided=[
"org.springframework.boot:spring-boot-dependencies:$globalSpringBootVersion",
]
//--这个是spring boot的测试框架,用testCompile导入
ref4SpringBootTest=[
"org.springframework.boot:spring-boot-starter-test:$globalSpringBootVersion"
]
//--spring框架api
ref4SpringFramework=[
"org.springframework:spring-web:$globalSpringVersion",
"org.springframework:spring-webmvc:$globalSpringVersion",
"org.springframework:spring-context-support:$globalSpringVersion",
"org.springframework.data:spring-data-jpa:$globalSpringDataJpaVersion",
"org.springframework:spring-test:$globalSpringVersion"
]
//--jsp&servlet等javaweb容器api,通常都用 compileOnly引用的。
ref4JspAndServletApi=[
"javax.servlet:javax.servlet-api:3.1.0",
"javax.servlet.jsp:jsp-api:2.2",
"javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"
]
//--jstl等java web的tag标准api,引入的话要用compile
ref4Jstl=[
'taglibs:standard:1.1.2',
'jstl:jstl:1.2'
]
//--mybatis
ref4MyBatis=[
"org.mybatis:mybatis:$globalMyBatisVersion"
]
//--这是apache common 类库引用的地址
ref4ApacheCommons = [
'commons-lang:commons-lang:2.6',
'commons-logging:commons-logging:1.2',
'commons-io:commons-io:2.5',
'commons-fileupload:commons-fileupload:1.3.2',
'commons-codec:commons-codec:1.10',
'commons-beanutils:commons-beanutils:1.9.3',
'commons-httpclient:commons-httpclient:3.1',
'org.apache.httpcomponents:fluent-hc:4.3.6',
'org.apache.httpcomponents:httpclient:4.5.3',
'org.apache.httpcomponents:httpclient-cache:4.5.3',
'org.apache.httpcomponents:httpcore:4.4.8',
'org.apache.httpcomponents:httpmime:4.5.3',
'org.apache.curator:curator-framework:4.0.1',
'org.jfree:jfreechart:1.0.19',
'org.apache.velocity:velocity:1.7',
'org.apache.poi:poi:3.16'
]
//--redis client
ref4RedisClient=["redis.clients:jedis:$globalRedisClientVersion"]
ref4Freemarker=["org.freemarker:freemarker:$globalFreemarkerVersion"]
//--这是阿里云短信引用的第三方类库
ref4AliYunSms=[
'com.aliyun:aliyun-java-sdk-core:3.2.8',
'com.aliyun:aliyun-java-sdk-dysmsapi:1.1.0'
]
//--阿里云图片裁剪
ref4AliSimpleImage=[
'com.alibaba:simpleimage:1.2.3'
]
//--阿里fast json引用地址
ref4FastJson=["com.alibaba:fastjson:$globalFastJsonVersion"]
//--json-lib引用地址
ref4JsonLib=["net.sf.json-lib:json-lib:2.4:jdk15"]
//--jdom1&jdom2以及相关api
ref4Jdom=[
'org.jdom:jdom2:2.0.6',
'org.jdom:jdom:1.1.3',
'joda-time:joda-time:2.9.7'
]
//--google guava
ref4GoogleGuava=["com.google.guava:guava:$globalGoogleGuavaVersion"]
//--dom4j
ref4Dom4j=["dom4j:dom4j:$globalDom4jVersion"]
ref4JavaMail=["javax.mail:mail:$globalJavaMailVersion"]
ref4Jsoup=["org.jsoup:jsoup:$globalJsoupVersion"]
ref4Quartz=[
"org.quartz-scheduler:quartz:$globalQuartzVersion",
"org.quartz-scheduler:quartz-jobs:$globalQuartzVersion"
]
ref4Flexmark=[
"com.vladsch.flexmark:flexmark-all:$globalFlexmarkVersion"
]
ref4PostgresqlJdbcDriver=[
"org.postgresql:postgresql:$globalPostgresqlJdbcDriverVersion"
]
ref4QiuniuSdkVersion=[
"com.qiniu:qiniu-java-sdk:$globalQiniuSdkVersion"
]
ref4ApacheAnt=["org.apache.ant:ant:$globalApacheAntVersion"]
//--二维码
ref4ZXing=[
"com.google.zxing:core:$globalGoogleZXingVersion",
"com.google.zxing:javase:$globalGoogleZXingVersion"
]
ref4FastdfsClient=["cn.bestwu:fastdfs-client-java:$globalFastdfsClientVersion"]
ref4Log4j=["log4j:log4j:$globalLog4jVersion"]
ref4Slf4jToLog4j=["org.slf4j:slf4j-log4j12:$globalSlf4jVersion"]
/****常见或者程序主要引用依赖定义 end****/
}
idea {
module {
inheritOutputDirs = true
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(GroovyCompile) {
groovyOptions.encoding = "MacRoman"
}
repositories {
maven{
//更换为阿里的仓库
url 'http://maven.aliyun.com/nexus/content/groups/public'
}
//有些jar包在中央仓库是没有的,需要手动添加上去
// flatDir { dirs 'local_jars' }
// mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
从中可以看到根目录的build.gradle中定义了各种类库版本以及依赖地址,
那么下面来看看子模块如何使用这些依赖:
以下代码来自实际应用的模块
group 'net.w2p'
version '1.0-SNAPSHOT'
dependencies {
// compile project(':cc-util')
//--servlet&jsp框架
compileOnly ref4JspAndServletApi
//--好了,这里不引入mybatis,防止冲突
compileOnly ref4MyBatis
//--json通用第三方库
compile ref4FastJson
compile ref4JsonLib
//--alibaba图片
compile ref4AliSimpleImage
//--阿里云短信
compile ref4AliYunSms
//apache commons
compile ref4ApacheCommons
//jdom
compile ref4Jdom
//guava-google
compile ref4GoogleGuava
//--日志
compile ref4Log4j
compile ref4Slf4jToLog4j
//--redis
compile ref4RedisClient
compile ref4Freemarker
//dom4j
compile ref4Dom4j
compile ref4JavaMail
compile ref4Jsoup
compile ref4Quartz
//java 翻译 markdown语法
compile ref4Flexmark
compile ref4PostgresqlJdbcDriver
//七牛
compile ref4QiuniuSdkVersion
//apache ant
compile ref4ApacheAnt
//--二维码
compile ref4ZXing
//--fastdfs客户端 - java版本
compile ref4FastdfsClient
// compile 'antlr:antlr:2.7.7'
// compile 'org.antlr:antlr4-runtime:4.1'
// compile 'javax.activation:activation:1.1.1'
/**
* compile 'javax.media:jai_core:1.1.3'
* 这个在各个仓库默认都是没有的,已经存放到本机jar中去了。
* **/
compile files('../libs/jai/jai_core-1.1.3.jar')
}
这样一来,感觉项目的第三方类库会变得很容易掌控。