2
votes

I try to configure log4j2 to use MongoDB in a gradle project, when I run in Eclipse and my tests it's work fine, but When I generate a jar and start it I get a error:

2018-08-13 09:30:11,534 main ERROR Unable to locate plugin type for MongoDb 2018-08-13 09:30:11,548 main ERROR Unable to locate plugin for MongoDb 2018-08-13 09:30:11,597 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender for element NoSql: java.lang.NullPointerException java.lang.NullPointerException at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.findNamedNode(PluginElementVisitor.java:103) at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:87) at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.injectFields(PluginBuilder.java:181) at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122) at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:959) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:899) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:891) at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:514) at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:238) at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:250) at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:547) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:619) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:581) at br.com.serversocket.main.Main.(Main.java:15)

2018-08-13 09:30:11,607 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender for element NoSql: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:235) at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135) at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:959) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:899) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:891) at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:514) at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:238) at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:250) at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:547) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:619) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:581) at br.com.serversocket.main.Main.(Main.java:15)

2018-08-13 09:30:11,617 main ERROR Null object returned for NoSql in appenders. 2018-08-13 09:30:11,655 main ERROR Unable to locate appender "NoSql" for logger config "root"

my log4j2.yaml:

Configuration:
  status: WARN
  appenders:    
    NoSql:
      name: NoSql
      MongoDb:
        databaseName: tracker
        collectionName: appLog
        server: ds261755.mlab.com
        port: 61755
        username: admin
        password: XXXXX
  Loggers:
    logger:
      -
        name: com.memorynotfound
        level: info
      -
        name: com.mchange.v2.c3p0
        level: warn
    Root:
      level: info
      AppenderRef:
        ref: NoSql

my buil.gradle:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
archivesBaseName = 'ServerSocket'
version = '3.2.1'

mainClassName = 'br.com.serversocket.main.Main'

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

compileJava {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

jar {
    dependsOn 'test'
    archiveName = "ServerSocket.jar"
    from {
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }

        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    manifest {
        attributes 'Main-Class': 'br.com.serversocket.main.Main'
    }

    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

test{
    useJUnitPlatform()
    dependsOn 'cleanTest'

    testLogging {
        events "passed", "skipped", "failed"
    }
    afterTest { desc, result -> 
        logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
    testLogging {
        exceptionFormat = 'full'
    }   
}

eclipse.classpath.file.whenMerged {
  entries.each { println it.path }
}
configurations { 
    providedCompile 
}

repositories {
    mavenCentral()
    mavenLocal()
}

ext {
    jacksonVersion = '2.9.1'
    hibernateVerison = '5.1.0.Final'
    slf4jVerison = '1.7.25'
    log4jVerison = '2.11.1'
    junitVersion = '5.2.0'
    emailVersion = '1.6.1'
}

dependencies {

    //compile files(fileTree(dir: 'lib', includes: ['*.jar']))  
    compile "org.hibernate:hibernate-jpamodelgen:${hibernateVerison}"
    compile "org.hibernate:hibernate-c3p0:${hibernateVerison}"
    compile "org.hibernate:hibernate-core:${hibernateVerison}"
    compile "org.hibernate:hibernate-entitymanager:${hibernateVerison}"

    compile "org.slf4j:slf4j-simple:${slf4jVerison}"
    compile "org.slf4j:slf4j-api:${slf4jVerison}"

    compile "org.apache.logging.log4j:log4j-slf4j-impl:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-api:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-core:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-nosql:2.9.1"
    compile "org.apache.logging.log4j:log4j-jcl:${log4jVerison}"


    compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
    compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
    compile "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
    compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}"

    compile "javax.mail:javax.mail-api:${emailVersion}"
    compile "com.sun.mail:javax.mail:${emailVersion}"

    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
    compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'

    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'
    compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '5.0.1.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    compile group: 'com.mchange', name: 'c3p0', version: '0.9.2.1'
    compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0'
    compile group: 'org.jboss.logging', name: 'jboss-logging', version: '3.3.1.Final'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.0.3'
    compile group: 'org.apache.mina', name: 'mina-core', version: '2.0.19'
    compile group: 'antlr', name: 'antlr', version: '2.7.7'
    compile group: 'com.fasterxml', name: 'classmate', version: '1.3.0'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
    compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.6'
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.5'
    compile group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.3'
    compile group: 'commons-discovery', name: 'commons-discovery', version: '0.2'
    compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
    compile group: 'net.sf.ehcache', name: 'ehcache', version: '1.5.0'
    compile group: 'org.apache.geronimo.specs', name: 'geronimo-jta_1.1_spec', version: '1.1.1'
    compile group: 'org.jboss', name: 'jandex', version: '2.0.3.Final'
    compile group: 'org.javassist', name: 'javassist', version: '3.22.0-GA'
    compile group: 'jdom', name: 'jdom', version: '1.1'
    compile group: 'javax.transaction', name: 'jta', version: '1.1'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
    compile group: 'org.apache.axis', name: 'axis', version: '1.4'
    compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'
    compile group: 'javax.xml.soap', name: 'saaj-api', version: '1.3'
    compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
    compile group: 'com.mchange', name: 'mchange-commons-java', version: '0.2.3.4'
    compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.8.0'


    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
    testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testCompile "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
    testCompile "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.2.0'
}

configurations {
    all*.exclude group: 'javassist', module: 'javassist'
}

My problem is in configuration? What I need to do?

[EDITED]

I change some configurations:

build.gradle:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
archivesBaseName = 'ServerSocket'
version = '3.2.1'

mainClassName = 'br.com.serversocket.main.Main'

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

compileJava {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

jar {
    //dependsOn 'test'
    archiveName = "ServerSocket.jar"
    from {
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }

        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    manifest {
        attributes 'Main-Class': 'br.com.serversocket.main.Main'
    }

    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

test{
    useJUnitPlatform()
    dependsOn 'cleanTest'

    testLogging {
        events "passed", "skipped", "failed"
    }
    afterTest { desc, result -> 
        logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
    testLogging {
        exceptionFormat = 'full'
    }   
}

eclipse.classpath.file.whenMerged {
  entries.each { println it.path }
}
configurations { 
    providedCompile 
}

repositories {
    mavenCentral()
    mavenLocal()
}

ext {
    jacksonVersion = '2.9.1'
    hibernateVerison = '5.1.0.Final'
    slf4jVerison = '1.7.25'
    log4jVerison = '2.11.1'
    junitVersion = '5.2.0'
    emailVersion = '1.6.1'
    mongoDBVersion = '3.8.0'
}

dependencies {

    //compile files(fileTree(dir: 'lib', includes: ['*.jar']))  
    compile "org.hibernate:hibernate-jpamodelgen:${hibernateVerison}"
    compile "org.hibernate:hibernate-c3p0:${hibernateVerison}"
    compile "org.hibernate:hibernate-core:${hibernateVerison}"
    compile "org.hibernate:hibernate-entitymanager:${hibernateVerison}"

    compile "org.slf4j:slf4j-simple:${slf4jVerison}"
    compile "org.slf4j:slf4j-api:${slf4jVerison}"

    compile "org.apache.logging.log4j:log4j-api:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-core:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-jcl:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-mongodb3:${log4jVerison}"
    //compile "org.apache.logging.log4j:log4j-nosql:${log4jVerison}"

    compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
    compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
    compile "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
    compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}"

    compile "javax.mail:javax.mail-api:${emailVersion}"
    compile "com.sun.mail:javax.mail:${emailVersion}"

    //compile "org.mongodb:mongo-java-driver:${mongoDBVersion}"
    compile "org.mongodb:mongodb-driver:${mongoDBVersion}"
    compile "org.mongodb:mongodb-driver-core:${mongoDBVersion}"
    compile "org.mongodb:bson:${mongoDBVersion}"


    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
    compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'

    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'
    compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '5.0.1.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    compile group: 'com.mchange', name: 'c3p0', version: '0.9.2.1'
    compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0'
    compile group: 'org.jboss.logging', name: 'jboss-logging', version: '3.3.1.Final'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.0.3'
    compile group: 'org.apache.mina', name: 'mina-core', version: '2.0.19'
    compile group: 'antlr', name: 'antlr', version: '2.7.7'
    compile group: 'com.fasterxml', name: 'classmate', version: '1.3.0'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
    compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.6'
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.5'
    compile group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.3'
    compile group: 'commons-discovery', name: 'commons-discovery', version: '0.2'
    compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
    compile group: 'net.sf.ehcache', name: 'ehcache', version: '1.5.0'
    compile group: 'org.apache.geronimo.specs', name: 'geronimo-jta_1.1_spec', version: '1.1.1'
    compile group: 'org.jboss', name: 'jandex', version: '2.0.3.Final'
    compile group: 'org.javassist', name: 'javassist', version: '3.22.0-GA'
    compile group: 'jdom', name: 'jdom', version: '1.1'
    compile group: 'javax.transaction', name: 'jta', version: '1.1'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
    compile group: 'org.apache.axis', name: 'axis', version: '1.4'
    compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'
    compile group: 'javax.xml.soap', name: 'saaj-api', version: '1.3'
    compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
    compile group: 'com.mchange', name: 'mchange-commons-java', version: '0.2.3.4'


    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
    testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testCompile "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
    testCompile "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.2.0'
}

configurations {
    all*.exclude group: 'javassist', module: 'javassist'
}

log4j2.yaml:

Configuration:
  status: WARN
  appenders:    
    NoSql:
      name: databaseAppender
      MongoDb3:
        databaseName: tracker
        collectionName: appLog
        server: ds261755.mlab.com
        port: 61755
        username: admin
        password: xxxx

  Loggers:
    logger:
      -
        name: com.memorynotfound
        level: info
      -
        name: com.mchange.v2.c3p0
        level: warn
    Root:
      level: info
      AppenderRef:
        ref: databaseAppender

and I get a new error message:

2018-08-14 10:29:26,690 main ERROR Unable to locate plugin type for MongoDb3
2018-08-14 10:29:26,703 main ERROR Unable to locate plugin for MongoDb3
2018-08-14 10:29:26,753 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender for element NoSql: java.lang.NullPointerException java.lang.NullPointerException
    at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.findNamedNode(PluginElementVisitor.java:103)
    at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:87)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.injectFields(PluginBuilder.java:181)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:959)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:899)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:891)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:514)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:238)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:250)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:547)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:619)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:581)
    at br.com.serversocket.main.Main.<clinit>(Main.java:15)

2018-08-14 10:29:26,764 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender for element NoSql: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.nosql.NoSqlAppender
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:235)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:959)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:899)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:891)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:514)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:238)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:250)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:547)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:619)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:581)
    at br.com.serversocket.main.Main.<clinit>(Main.java:15)

2018-08-14 10:29:26,773 main ERROR Null object returned for NoSql in appenders.
2018-08-14 10:29:26,813 main ERROR Unable to locate appender "databaseAppender" for logger config "root"

I inspected class' log4j-core to try understend better about the error and found this method:

protected void preConfigure(final Node node) {
    try {
        for (final Node child : node.getChildren()) {
            if (child.getType() == null) {// problem happen here in production
                LOGGER.error("Unable to locate plugin type for " + child.getName());
                continue;
            }
            final Class<?> clazz = child.getType().getPluginClass();
            if (clazz.isAnnotationPresent(Scheduled.class)) {
                configurationScheduler.incrementScheduledItems();
            }
            preConfigure(child);
        }
    } catch (final Exception ex) {
        LOGGER.error("Error capturing node data for node " + node.getName(), ex);
    }
} 

When I run project in IDE a node's property "getType()" is "PluginType [pluginClass=class org.apache.logging.log4j.mongodb3.MongoDbProvider, key=mongodb3, elementName=MongoDb3, isObjectPrintable=true, isDeferChildren==false, category=core]" but I production, I suppose how the jar not found a class inside log4j-mongodb3. I inspect generated jar, but the class was packaged without problems. what is happen ?

[EDITED]

I found my problem, I created a test project without gradle and I generated a jar, I need to copy dependencies and not extract they, how can I configure gradle to do it in jar task ?

1
please use code area to clean exception and code format for reading.amir azizkhani

1 Answers

2
votes

I solved my problem using 2 gradle's plugin:

shadow: http://imperceptiblethoughts.com/shadow/#shadowing_gradle_plugins

and this plugin to fix a incompability with log4j:

https://github.com/TheBoegl/shadow-log4j-transformer#using-the-gradle-plugin-dsl

follow my build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
        classpath 'de.sebastianboegl.gradle.plugins:shadow-log4j-transformer:2.2.0'
    }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'com.github.johnrengelman.shadow'
archivesBaseName = 'ServerSocket'
version = '3.2.1'

mainClassName = 'br.com.serversocket.main.Main'

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

compileJava {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

shadowJar {
    //dependsOn 'test'
    transform(de.sebastianboegl.gradle.plugins.shadow.transformers.Log4j2PluginsFileTransformer)
    archiveName = "ServerSocket.jar"

    manifest {
        attributes 'Main-Class': 'br.com.serversocket.main.Main'
    }

    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

jar {
    //dependsOn 'test'
    archiveName = "ServerSocket.jar"
    from {
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }

        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    manifest {
        attributes 'Main-Class': 'br.com.serversocket.main.Main'
    }

    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

test{
    useJUnitPlatform()
    dependsOn 'cleanTest'

    testLogging {
        events "passed", "skipped", "failed"
    }
    afterTest { desc, result -> 
        logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
    testLogging {
        exceptionFormat = 'full'
    }   
}

eclipse.classpath.file.whenMerged {
  entries.each { println it.path }
}
configurations { 
    providedCompile 
}

repositories {
    mavenCentral()
    mavenLocal()
}

ext {
    jacksonVersion = '2.9.1'
    hibernateVerison = '5.1.0.Final'
    slf4jVerison = '1.7.25'
    log4jVerison = '2.11.1'
    junitVersion = '5.2.0'
    emailVersion = '1.6.1'
    mongoDBVersion = '3.8.0'
}

dependencies {

    //compile files(fileTree(dir: 'lib', includes: ['*.jar']))  
    compile "org.hibernate:hibernate-jpamodelgen:${hibernateVerison}"
    compile "org.hibernate:hibernate-c3p0:${hibernateVerison}"
    compile "org.hibernate:hibernate-core:${hibernateVerison}"
    compile "org.hibernate:hibernate-entitymanager:${hibernateVerison}"

    compile "org.slf4j:slf4j-simple:${slf4jVerison}"
    compile "org.slf4j:slf4j-api:${slf4jVerison}"

    compile "org.apache.logging.log4j:log4j-api:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-core:${log4jVerison}"
    compile "org.apache.logging.log4j:log4j-mongodb3:${log4jVerison}"


    compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
    compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
    compile "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
    compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}"

    compile "javax.mail:javax.mail-api:${emailVersion}"
    compile "com.sun.mail:javax.mail:${emailVersion}"

    compile "org.mongodb:mongodb-driver:${mongoDBVersion}"
    compile "org.mongodb:mongodb-driver-core:${mongoDBVersion}"
    compile "org.mongodb:bson:${mongoDBVersion}"


    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
    compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'

    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'
    compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '5.0.1.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    compile group: 'com.mchange', name: 'c3p0', version: '0.9.2.1'
    compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0'
    compile group: 'org.jboss.logging', name: 'jboss-logging', version: '3.3.1.Final'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.0.3'
    compile group: 'org.apache.mina', name: 'mina-core', version: '2.0.19'
    compile group: 'antlr', name: 'antlr', version: '2.7.7'
    compile group: 'com.fasterxml', name: 'classmate', version: '1.3.0'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
    compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.6'
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.5'
    compile group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.3'
    compile group: 'commons-discovery', name: 'commons-discovery', version: '0.2'
    compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
    compile group: 'net.sf.ehcache', name: 'ehcache', version: '1.5.0'
    compile group: 'org.apache.geronimo.specs', name: 'geronimo-jta_1.1_spec', version: '1.1.1'
    compile group: 'org.jboss', name: 'jandex', version: '2.0.3.Final'
    compile group: 'org.javassist', name: 'javassist', version: '3.22.0-GA'
    compile group: 'jdom', name: 'jdom', version: '1.1'
    compile group: 'javax.transaction', name: 'jta', version: '1.1'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
    compile group: 'org.apache.axis', name: 'axis', version: '1.4'
    compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'
    compile group: 'javax.xml.soap', name: 'saaj-api', version: '1.3'
    compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
    compile group: 'com.mchange', name: 'mchange-commons-java', version: '0.2.3.4'


    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
    testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testCompile "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
    testCompile "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.2.0'
}

configurations {
    all*.exclude group: 'javassist', module: 'javassist'
}