From eec4fb5aec2b07c56e89221650382559a2e9a087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Thu, 24 Jan 2019 12:32:00 +0800 Subject: [PATCH] :tada: 0.0.1-RC1 --- build.gradle | 15 ++- gradle.properties | 9 ++ gradle/publish-bom.gradle | 48 ++++++++ gradle/publish-jar.gradle | 103 +++++++++++++++++ gradle/publish-maven.gradle | 107 ------------------ mica-bom/build.gradle | 25 +--- mica-bom/mica-bom.txt | 1 - .../mica/error/MicaExceptionTranslator.java | 80 +++++++++++++ .../mica/error/RestExceptionTranslator.java | 54 +-------- 9 files changed, 257 insertions(+), 185 deletions(-) create mode 100644 gradle/publish-bom.gradle create mode 100644 gradle/publish-jar.gradle delete mode 100644 gradle/publish-maven.gradle delete mode 100644 mica-bom/mica-bom.txt create mode 100644 mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java diff --git a/build.gradle b/build.gradle index ba822c30..6a60fc16 100644 --- a/build.gradle +++ b/build.gradle @@ -25,13 +25,14 @@ configure(subprojects) { group = GROUPID version = VERSION apply plugin: "java-library" - apply plugin: "maven" + apply plugin: 'signing' + apply plugin: 'maven-publish' apply plugin: "io.spring.dependency-management" - apply from: "${rootProject.projectDir}/gradle/publish-maven.gradle" - apply from: "${rootProject.projectDir}/gradle/quality.gradle" } configure(subprojects - project(":mica-bom")) { + apply from: "${rootProject.projectDir}/gradle/publish-jar.gradle" + apply from: "${rootProject.projectDir}/gradle/quality.gradle" sourceCompatibility = "$javaVersion" targetCompatibility = "$javaVersion" tasks.withType(JavaCompile) { @@ -73,3 +74,11 @@ configure(subprojects - project(":mica-bom")) { compileJava.dependsOn(processResources) } + +def getRepositoryUsername() { + return hasProperty('NEXUS_OSS_USER_NAME') ? NEXUS_OSS_USER_NAME : "" +} + +def getRepositoryPassword() { + return hasProperty('NEXUS_OSS_PASS_WORD') ? NEXUS_OSS_PASS_WORD : "" +} diff --git a/gradle.properties b/gradle.properties index 9cddf3ff..51a2b3fb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,3 +10,12 @@ REPOSITORY_URL_RELEASE=https://oss.sonatype.org/service/local/staging/deploy/mav signing.keyId=D73A4CFE signing.password=sonatype signing.secretKeyRingFile=/Users/lcm/.gnupg/secring.gpg + +POM_NAME=mica +POM_DESCRIPTION=An enhanced toolkit of Spring cloud to simplify development. +GIT_URL=https://github.com/lets-mica/mica +GIT_SCM_CONNECTION=scm:github.com/lets-mica/mica +LICENSE_NAME=GNU LESSER GENERAL PUBLIC LICENSE +LICENSE_URL=https://www.gnu.org/licenses/lgpl-3.0.en.html +DEVELOPER_NAME=Dreamlu +DEVELOPER_EMAIL=qq596392912@gmail.com diff --git a/gradle/publish-bom.gradle b/gradle/publish-bom.gradle new file mode 100644 index 00000000..2b65093a --- /dev/null +++ b/gradle/publish-bom.gradle @@ -0,0 +1,48 @@ +tasks.whenTaskAdded { task -> + if (task.name.contains('signMavenJavaPublication')) { + task.enabled = new File(project.property('signing.secretKeyRingFile') as String).isFile() + } +} + +publishing { + repositories { + maven { + url = version.endsWith('SNAPSHOT') ? REPOSITORY_URL_SNAPSHOT : REPOSITORY_URL_RELEASE + credentials { + username getRepositoryUsername() + password getRepositoryPassword() + } + } + } + publications { + mavenJava(MavenPublication) { + pom { + name = POM_NAME + description = POM_DESCRIPTION + url = GIT_URL + + scm { + connection = GIT_SCM_CONNECTION + developerConnection = GIT_SCM_CONNECTION + url = GIT_URL + } + licenses { + license { + name = LICENSE_NAME + url = LICENSE_URL + } + } + developers { + developer { + name = DEVELOPER_NAME + email = DEVELOPER_EMAIL + } + } + } + } + } + + signing { + sign publishing.publications.mavenJava + } +} diff --git a/gradle/publish-jar.gradle b/gradle/publish-jar.gradle new file mode 100644 index 00000000..723b6a72 --- /dev/null +++ b/gradle/publish-jar.gradle @@ -0,0 +1,103 @@ +jar { + afterEvaluate { + manifest { + attributes 'Implementation-Version': version + } + } +} + +task sourcesJar(type: Jar) { + archiveClassifier.set('sources') + from sourceSets.main.allSource +} + +javadoc { + options { + encoding "UTF-8" + charSet 'UTF-8' + author true + version true + failOnError false + links "http://docs.oracle.com/javase/8/docs/api" + } +} + +task javadocJar(type: Jar) { + archiveClassifier.set('javadoc') + from 'build/docs/javadoc' +} + +artifacts { + archives sourcesJar + archives javadocJar +} + +tasks.whenTaskAdded { task -> + if (task.name.contains('signMavenJavaPublication')) { + task.enabled = new File(project.property('signing.secretKeyRingFile') as String).isFile() + } +} + +publishing { + repositories { + maven { + url = version.endsWith('SNAPSHOT') ? REPOSITORY_URL_SNAPSHOT : REPOSITORY_URL_RELEASE + credentials { + username getRepositoryUsername() + password getRepositoryPassword() + } + } + } + publications { + mavenJava(MavenPublication) { + from components.java + + artifact sourcesJar + artifact javadocJar + + pom { + name = POM_NAME + description = POM_DESCRIPTION + url = GIT_URL + + scm { + connection = GIT_SCM_CONNECTION + developerConnection = GIT_SCM_CONNECTION + url = GIT_URL + } + licenses { + license { + name = LICENSE_NAME + url = LICENSE_URL + } + } + developers { + developer { + name = DEVELOPER_NAME + email = DEVELOPER_EMAIL + } + } + + withXml { + def root = asNode() + root.dependencies.'*'.findAll { + def d = it + d.scope.text() == 'runtime' && project.configurations + .findByName("implementation").allDependencies.find { dep -> + dep.name == it.artifactId.text() + }.each() { + d.scope*.value = 'compile' + d.appendNode('optional', true) + } + } + } + } + } + } + + signing { + sign publishing.publications.mavenJava + } +} + + diff --git a/gradle/publish-maven.gradle b/gradle/publish-maven.gradle deleted file mode 100644 index 9bc5efae..00000000 --- a/gradle/publish-maven.gradle +++ /dev/null @@ -1,107 +0,0 @@ -apply plugin: 'signing' -apply plugin: 'maven-publish' - -publishing { - publications { - mavenJava(MavenPublication) { - from components.java - } - } -} - -jar { - into("META-INF/maven/$project.group/$project.name") { - from { generatePomFileForMavenJavaPublication } - rename ".*", "pom.xml" - } -} - -task sourcesJar(type: Jar) { - archiveClassifier.set('sources') - from sourceSets.main.allSource -} - -javadoc { - options { - encoding "UTF-8" - charSet 'UTF-8' - author true - version true - failOnError false - links "http://docs.oracle.com/javase/8/docs/api" - } -} - -task javadocJar(type: Jar) { - archiveClassifier.set('javadoc') - from 'build/docs/javadoc' -} - -artifacts { - archives sourcesJar - archives javadocJar -} - -uploadArchives { - repositories.mavenDeployer { - pom.version = "${VERSION}" - pom.artifactId = "$project.name" - pom.groupId = "$project.group" - - signing { - sign configurations.archives - } - beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - repository(url: pom.version.endsWith('SNAPSHOT') - ? REPOSITORY_URL_SNAPSHOT : REPOSITORY_URL_RELEASE) { - authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) - } - - pom.project { - name = 'mica' - packaging = 'jar' - description = 'An enhanced toolkit of Spring cloud to simplify development.' - url = 'https://github.com/lets-mica/mica' - - scm { - connection = 'scm:github.com/lets-mica/mica' - developerConnection = 'scm:git@github.com/lets-mica/mica' - url = 'https://github.com/lets-mica/mica' - } - licenses { - license { - name = 'GNU LESSER GENERAL PUBLIC LICENSE' - url = 'https://www.gnu.org/licenses/lgpl-3.0.en.html' - } - } - developers { - developer { - name = 'Dreamlu' - email = 'qq596392912@gmail.com' - } - } - } - - pom.withXml { - def root = asNode() - root.dependencies.'*'.findAll { - def d = it - d.scope.text() == 'runtime' && project.configurations - .findByName("implementation").allDependencies.find { dep -> - dep.name == it.artifactId.text() - }.each() { - d.scope*.value = 'compile' - d.appendNode('optional', true) - } - } - } - } -} - -def getRepositoryUsername() { - return hasProperty('NEXUS_OSS_USER_NAME') ? NEXUS_OSS_USER_NAME : "" -} - -def getRepositoryPassword() { - return hasProperty('NEXUS_OSS_PASS_WORD') ? NEXUS_OSS_PASS_WORD : "" -} diff --git a/mica-bom/build.gradle b/mica-bom/build.gradle index eabd8572..329d6d1a 100644 --- a/mica-bom/build.gradle +++ b/mica-bom/build.gradle @@ -1,8 +1,4 @@ -configurations.archives.artifacts.clear() - -artifacts { - archives(file("mica-bom.txt")) -} +apply from: "${rootProject.projectDir}/gradle/publish-bom.gradle" dependencyManagement { dependencies { @@ -12,26 +8,7 @@ dependencyManagement { dependency "net.dreamlu:mica-log4j2:${VERSION}" dependency "net.dreamlu:mica-boot:${VERSION}" dependency "net.dreamlu:mica-boot-test:${VERSION}" - dependency "net.dreamlu:mica-cloud:${VERSION}" - dependency "net.dreamlu:mica-error-catch-client:${VERSION}" - dependency "net.dreamlu:mica-cache-redis:${VERSION}" - dependency "net.dreamlu:mica-data-mongo:${VERSION}" dependency "net.dreamlu:mica-captcha:${VERSION}" } } -install { - repositories.mavenInstaller { - pom.whenConfigured { - packaging = "pom" - } - } -} - -uploadArchives { - repositories.mavenDeployer { - pom.whenConfigured { - packaging = "pom" - } - } -} diff --git a/mica-bom/mica-bom.txt b/mica-bom/mica-bom.txt deleted file mode 100644 index 8d83794a..00000000 --- a/mica-bom/mica-bom.txt +++ /dev/null @@ -1 +0,0 @@ -占位文件 diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java new file mode 100644 index 00000000..52786951 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java @@ -0,0 +1,80 @@ +package net.dreamlu.mica.error; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.core.exception.ServiceException; +import net.dreamlu.mica.core.result.R; +import net.dreamlu.mica.core.result.SystemCode; +import net.dreamlu.mica.core.utils.Exceptions; +import net.dreamlu.mica.core.utils.ObjectUtil; +import net.dreamlu.mica.core.utils.WebUtil; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.DispatcherServlet; + +import javax.servlet.Servlet; +import javax.servlet.http.HttpServletRequest; + +/** + * mica 未知异常转译和发送,方便监听,对未知异常统一处理。Order 排序优先级低 + * + * @author L.cm + */ +@Slf4j +@Order +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) +@RestControllerAdvice +@AllArgsConstructor +public class MicaExceptionTranslator { + private final ApplicationEventPublisher publisher; + + @ExceptionHandler(ServiceException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public R handleError(ServiceException e) { + log.error("业务异常", e); + R result = e.getResult(); + if (result == null) { + // 发送:未知业务异常事件 + result = R.fail(SystemCode.FAILURE, e.getMessage()); + publishEvent(e); + } + return result; + } + + @ExceptionHandler(Throwable.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public R handleError(Throwable e) { + log.error("未知异常", e); + // 发送:未知异常异常事件 + publishEvent(e); + return R.fail(SystemCode.FAILURE); + } + + private void publishEvent(Throwable error) { + HttpServletRequest request = WebUtil.getRequest(); + MicaErrorEvent event = new MicaErrorEvent(); + event.setRequestUrl(request.getRequestURI()); + event.setStackTrace(Exceptions.getStackTraceAsString(error)); + event.setExceptionName(error.getClass().getName()); + event.setMessage(error.getMessage()); + StackTraceElement[] elements = error.getStackTrace(); + if (ObjectUtil.isNotEmpty(elements)) { + StackTraceElement element = elements[0]; + event.setClassName(element.getClassName()); + event.setFileName(element.getFileName()); + event.setMethodName(element.getMethodName()); + event.setLineNumber(element.getLineNumber()); + } + // 发布事件 + publisher.publishEvent(event); + } +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java index 104a3b8a..26b75fee 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java @@ -16,20 +16,16 @@ package net.dreamlu.mica.error; -import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import net.dreamlu.mica.core.exception.ServiceException; import net.dreamlu.mica.core.result.R; import net.dreamlu.mica.core.result.SystemCode; -import net.dreamlu.mica.core.utils.Exceptions; -import net.dreamlu.mica.core.utils.ObjectUtil; import net.dreamlu.mica.core.utils.StringUtil; -import net.dreamlu.mica.core.utils.WebUtil; import org.hibernate.validator.internal.engine.path.PathImpl; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindException; @@ -48,13 +44,12 @@ import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.NoHandlerFoundException; import javax.servlet.Servlet; -import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.Set; /** - * 全局异常处理,处理可预见的异常 + * 全局异常处理,处理可预见的异常,Order 排序优先级高 * *

* Bean-Violation 异常处理 @@ -63,13 +58,12 @@ import java.util.Set; * @author L.cm */ @Slf4j +@Order(Ordered.HIGHEST_PRECEDENCE) @Configuration @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) @RestControllerAdvice -@AllArgsConstructor public class RestExceptionTranslator { - private final ApplicationEventPublisher eventPublisher; @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @@ -154,44 +148,4 @@ public class RestExceptionTranslator { return R.fail(SystemCode.MEDIA_TYPE_NOT_SUPPORTED, message); } - @ExceptionHandler(ServiceException.class) - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public R handleError(ServiceException e) { - log.error("业务异常", e); - R result = e.getResult(); - if (result == null) { - // 发送:未知业务异常事件 - result = R.fail(SystemCode.FAILURE, e.getMessage()); - publishEvent(e); - } - return result; - } - - @ExceptionHandler(Throwable.class) - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public R handleError(Throwable e) { - log.error("未知异常", e); - // 发送:未知异常异常事件 - publishEvent(e); - return R.fail(SystemCode.FAILURE); - } - - private void publishEvent(Throwable error) { - HttpServletRequest request = WebUtil.getRequest(); - MicaErrorEvent event = new MicaErrorEvent(); - event.setRequestUrl(request.getRequestURI()); - event.setStackTrace(Exceptions.getStackTraceAsString(error)); - event.setExceptionName(error.getClass().getName()); - event.setMessage(error.getMessage()); - StackTraceElement[] elements = error.getStackTrace(); - if (ObjectUtil.isNotEmpty(elements)) { - StackTraceElement element = elements[0]; - event.setClassName(element.getClassName()); - event.setFileName(element.getFileName()); - event.setMethodName(element.getMethodName()); - event.setLineNumber(element.getLineNumber()); - } - // 发布事件 - eventPublisher.publishEvent(event); - } } -- GitLab