[Gradle]使用远程maven仓库时候出现 unindexed 异常+idea无法识别gradle的导入+打包jar到nexus+构建全局设定优化+copy文件乱码+

unindexed##

请升级你的idea,太旧了,升级到比较新的版本就没事。

idea识别不了gradle的jar

例如:
这里写图片描述

这里写图片描述

解决方案,请在build.gradle添加

apply plugin: 'idea'

然后执行 idea任务:
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

打包jar到nexus

gradle 打包jar上传到nexus 同时上传源码jar

假设我们的一个api项目是这样的:
这个项目需要提供给其他服务调用:
这里写图片描述

于是我们的build.gradle内容如下:

apply plugin: 'java'
apply plugin: 'idea'
//打包jar以后可以上传到远程nexus服务器
apply plugin: "maven-publish"
sourceCompatibility = 1.8
version = '1.0'
repositories {
    maven{
        url  '你自己的maven仓库地址'
    }
}
dependencies {
    compile group:'net.funfunle',name:'baselib',version:'1.0.0-SNAPSHOT'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

//设置动态属性
ext {
    //发布到仓库用户名
    publishUserName = "admin"
    //发布到仓库地址
    publishUserPassword = "我就是密码密码就是我"
    //仓库地址
    publishURL="你自己的maven仓库发布地址"

    apiBaseJarName = "DevBase-Api"
    apiBaseJarVersion = '1.0.0-SNAPSHOT'
    builtBy="gradle 4.10.1"
}
//jar包名称组成:[baseName]-[appendix]-[version]-[classifier].[extension]
//打包class文件
task apiBaseJar(type:Jar){
    version apiBaseJarVersion
    baseName apiBaseJarName
    from sourceSets.main.output
    destinationDir file("$buildDir/api-libs")
    includes ['net/w2p/DevBase/**']
    manifest {
        attributes 'packageName': apiBaseJarName, 'Built-By': builtBy,
                'Built-date': new Date().format('yyyy-MM-dd HH:mm:ss'),
                'Manifest-Version':version
    }
}

//上传jar包
publishing {
    publications {
        publishing.publications.create('apiBase', MavenPublication) {
            groupId 'net.w2p.DevBase'
            artifactId apiBaseJarName
            version apiBaseJarVersion
            artifact apiBaseJar
        }
    }
    repositories {
        maven {
            url  publishURL
            credentials {
                username = publishUserName
                password = publishUserPassword
            }
        }
    }
}



首先,执行 gradle tasks,查看所有任务。
这里写图片描述

然后可以看到:

17:22:24: Executing task 'tasks'...


> Task :Api:tasks

------------------------------------------------------------
All tasks runnable from project :Api
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in project ':Api'.
components - Displays the components produced by project ':Api'. [incubating]
dependencies - Displays all dependencies declared in project ':Api'.
dependencyInsight - Displays the insight into a specific dependency in project ':Api'.
dependentComponents - Displays the dependent components of components in project ':Api'. [incubating]
help - Displays a help message.
model - Displays the configuration model of project ':Api'. [incubating]
projects - Displays the sub-projects of project ':Api'.
properties - Displays the properties of project ':Api'.
tasks - Displays the tasks runnable from project ':Api'.

IDE tasks
---------
cleanIdea - Cleans IDEA project files (IML, IPR)
idea - Generates IDEA project files (IML, IPR, IWS)

Publishing tasks
----------------
generateMetadataFileForApiBasePublication - Generates the Gradle metadata file for publication 'apiBase'.
generatePomFileForApiBasePublication - Generates the Maven POM file for publication 'apiBase'.
publish - Publishes all publications produced by this project.
publishApiBasePublicationToMavenLocal - Publishes Maven publication 'apiBase' to the local Maven repository.
publishApiBasePublicationToMavenRepository - Publishes Maven publication 'apiBase' to Maven repository 'maven'.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
17:22:24: Task execution finished 'tasks'.


我们看到它多了几个任务—这玩意会动态生成任务的啊。。。
其中,

publishApiBasePublicationToMavenRepository

这个就是我们要用到的任务了,可以发布到线上仓库。
不急,
首先运行

apiBaseJar

看看结果:
这里写图片描述

这里写图片描述

好了,本地的打包没问题,那么执行:

publishApiBasePublicationToMavenRepository

这里写图片描述

这里写图片描述

然后看看nexus服务器的情况。

这里写图片描述

好了,已经上传成功了,现在,为了能够简化一下发布jar的流程,改一下脚本。在后面添加:

task distribution(dependsOn: 'publishApiBasePublicationToMavenRepository') << {
    println '==============>>>>jar发布完成'
}

验证:
这里写图片描述

这里写图片描述

这里写图片描述

好了,jar的发布就到这里了、。

项目全局设定-gradle

Gradle配置全局变量

这位兄弟很有道理。。

好了,我这边稍微利用上面文章改造一下gradle项目。

首先将config.gradle提取出来:

这里写图片描述

内容如下:
因为这些配置各个项目都不一样,每个人不同,所以就截图算了。

这里写图片描述

然后主项目中,build.gradle导入:

这里写图片描述

apply plugin: 'java'
apply from : 'config.gradle'
version = '1.0'
/***所有项目共通***/
allprojects {
    sourceCompatibility=1.8
    apply plugin: 'java'
    apply plugin: 'idea'
    repositories {
        maven{
            url  '你的maven仓库地址'
        }
    }
    dependencies {
        compile group:'net.funfunle',name:'baselib',version:'1.0.0-SNAPSHOT'
        testCompile group: 'junit', name: 'junit', version: '4.11'
    }
}

在api子项目中,现在的build.gradle代码如下:

//打包jar以后可以上传到远程nexus服务器
apply plugin: "maven-publish"
version = '1.0'
dependencies {
    compile group:'net.funfunle',name:'baselib',version:'1.0.0-SNAPSHOT'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

//设置动态属性
def repoConf = rootProject.ext.repoConf

//jar包名称组成:[baseName]-[appendix]-[version]-[classifier].[extension]
//打包class文件
task apiBaseJar(type:Jar){
    version repoConf.apiBaseJarVersion
    baseName repoConf.apiBaseJarName
    from sourceSets.main.output
    destinationDir file("$buildDir/api-libs")
    includes ['net/w2p/DevBase/**']
    manifest {
        attributes 'packageName': repoConf.apiBaseJarName, 'Built-By': repoConf.builtBy,
                'Built-date': new Date().format('yyyy-MM-dd HH:mm:ss'),
                'Manifest-Version':version
    }
}

//上传jar包
publishing {
    publications {
        publishing.publications.create('apiBase', MavenPublication) {
            groupId 'net.w2p.DevBase'
            artifactId repoConf.apiBaseJarName
            version repoConf.apiBaseJarVersion
            artifact apiBaseJar
        }
    }
    repositories {
        maven {
            url  repoConf.publishURL
            credentials {
                username = repoConf.publishUserName
                password = repoConf.publishUserPassword
            }
        }
    }
}


task distribution(dependsOn: 'publishApiBasePublicationToMavenRepository') << {
    println '==============>>>>jar发布完成'
}

好了,运行。。
这里写图片描述

也是没问题。。

copy文件乱码

场景:
这里写图片描述

复制然后替换掉变量以后,结果发现中文都变乱码了。

原文件:

这里写图片描述

现文件:

这里写图片描述
解决方案:

解决: 修改GRADLE_HOME/bin/gradle(windows系统中是gradle.bat)中的变量DEFAULT_JVM_OPTS
DEFAULT_JVM_OPTS="-Dfile.encoding=UTF-8"

即,在这个文件,

这里写图片描述

改为:

这里写图片描述
然后,

gradle compileConfig

可以看到:

这里写图片描述

idea编译运行spring项目没有导入spring context等lib进去。

请看:
build.gradle中已经有依赖:

在这里插入图片描述

而在idea的项目依赖里面看到几个jar都是红色不可用状态:

在这里插入图片描述

在编译过后,查看输出目录的libs可以看到:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

-_- 确实没有spring - context等等jar啊。。
不管了,继续访问页面,会出现:

在这里插入图片描述

所以,问题在于,idea到底经历了什么竟然连spring context这些jar都下载不下来。。。

解决方案

idea-bug,resources资源文件无法复制bug

IDEA 13、14 配合 gradle 时候无法正确编译资源文件的 bug

照抄:

问题说明
IDEA 确实是个折腾人的玩意,没想到在最基本的编译环节居然也能遇到问题。

此次配置一个单module 的项目,使用的是 gradle 导入项目,没想到发现编译之后允许某个单元测试的时候出错了,到 build 目录一看,感情是 resources 目录下的配置文件都没用拷贝过来,只有一堆的 class package。

Google 一圈发现这是 IDEA 的 bug,不过官方 issue 里面不愿意承认是 bug:
IDEA-128273 Resource folders drop out of junit test classpath after a while for Gradle based project

这个问题据说也牵连到了 Android Studio。

官方提供的解决办法
给出的解决办法是在 gradle 的 IDEA 配置中增加一行:

idea {
    module {
        inheritOutputDirs = true
    }
}

在这里插入图片描述

这时候还需要注意一点,尽量不要手贱(例如我),在这儿又设置了下项目名称:

idea {
    module {
        name = "XXOO"
        inheritOutputDirs = true
    }
}

这么搞的话输出又会出现问题了,奶奶的,IDEA 你还敢说这不是你们的 bug,起码是处理不严谨吧!!!

** ❗️ 另外采用这种办法的话,需要删除项目下的 IDEA 相关配置文件,然后重新导入。**

民间智慧

在stackoverflow 发现的解决办法 是在 gradle 中多配置个 Task:

task copyTestResources(type: Copy) {
    from "${projectDir}/src/test/resources"
    into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources

意思很简单,就是在处理 Test 资源的时候将这些 resources 下的文件拷贝到相应的 build 下。

不过这种方法有点麻烦,单项目比较简单,直接用就行了,但是如果是多 module 的话还得进行相应处理。

结论

这个无论如何我觉得都算是 IDEA 的bug 了,不过目前既然有了解决方案起码不算是太影响干活。

另外推荐采用官方提供的解决方案,相对来说省事,也能顺便解决多 module 的问题。

目前表现

在这里插入图片描述

在这里插入图片描述

emmmmm,解决方案:

idea新bug,jsp模板页面无法识别其他模块

如下图:

在这里插入图片描述

解决方案:

1、我们注意到,直接引用jar文件,无论是本地还是远程的都不会有问题,
2、其次,我们注意到,几个模块都有自己的jar文件。
在这里插入图片描述

所以我们可以,这样解决:

在这里插入图片描述

然后,运行idea,

在这里插入图片描述

最后,

在这里插入图片描述

可以正常提示了。

gradle的profile实现

gradle中并没有直接类似maven中的profile支持,只能变通的用其它方法来处理,在打包不同环境的应用时,通常会遇到二类问题:

一、不同的环境依赖的jar包不同

拿web开发来说,生产环境一般会采用weblogic,jboss这类重量级的容器,通常这类web server已经内置了很多第三方的通用jar包,而开发环境,一般采用嵌入式jetty这类轻量级的容器,内置的jar包会少一些,在maven中可以用provided来处理,到了gradle中可以这么处理:

build.gradle文件参考下面的写法:

def env = System.getProperty(“env”) ?: “dev”

apply from: “profile-${env}.gradle”
大概意思是,根据传入的参数env不同,加载不同的profile文件。在同级目录下,要放二个文件(下面演示的场景为,dev环境加载的spring版本为4.1.6,而prod环境加载的spring版本为4.2.3):

profile-dev.gradle

复制代码
dependencies {
compile ‘org.springframework:spring-core:4.1.6.RELEASE’
compile ‘org.springframework:spring-beans:4.1.6.RELEASE’
compile ‘org.springframework:spring-context:4.1.6.RELEASE’
compile ‘org.springframework:spring-context-support:4.1.6.RELEASE’
compile ‘org.springframework:spring-aop:4.1.6.RELEASE’
}
复制代码
profile-prod.gradle

dependencies {
compile ‘org.springframework:spring-core:4.2.3.RELEASE’
compile ‘org.springframework:spring-beans:4.2.3.RELEASE’
compile ‘org.springframework:spring-context:4.2.3.RELEASE’
}
编译时,gradle命令这么写:

gradle build -Denv=prod 这样编译的就是prod环境

gradle build -Denv=dev 这样编译的就是dev环境(注:dev是默认环境,所以如果是dev环境,最后的-Denv=dev也可以省略)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值