java - Gradle does not find dependencies -
i trying use gradle , file looks like:
// apply java plugin add support java apply plugin: 'java' // in section declare find dependencies of project repositories { // use 'jcenter' resolving dependencies. // can declare maven/ivy/file repository here. mavencentral() jcenter() } jar { manifest { attributes 'main-class': 'execute.entry' } } // in section declare dependencies production , test code dependencies { // production code uses slf4j logging api @ compile time compile 'org.slf4j:slf4j-api:1.7.12' compile 'org.apache.logging.log4j:log4j:2.3' // declare dependency favourite test framework want use in tests. // testng supported gradle test task. change // testcompile dependency testcompile 'org.testng:testng:6.8.1' , add // 'test.usetestng()' build script. testcompile 'junit:junit:4.12' } as can see, i've added 2 dependencies , want use in class
package execute; import message.*; import org.apache.log4j.logger; public class entry { public static void main(string[] args) { service s = new service(); string msg = s.getmessage(); logger.info("received msg: " + msg); } } when execute statement gradle assemble, i've got compiler error.
d:\java\entrypoint\src\main\java\execute\entry.java:4: error: package org.apache.log4j not exist import org.apache.log4j.logger; ^ d:\java\entrypoint\src\main\java\execute\entry.java:12: error: cannot find symbol logger.info("received msg: " + msg); ^ symbol: variable logger location: class entry 2 errors :compilejava failed failure: build failed exception. * went wrong: execution failed task ':compilejava'. > compilation failed; see compiler error output details. * try: run --stacktrace option stack trace. run --info or --debug option more log output. build failed total time: 6.261 secs compilation failed; see compiler error output details. what doing wrong?
update changed code this:
package execute; import message.*; import org.apache.logging.log4j.logger; import org.apache.logging.log4j.logmanager; public class entry { private static final logger logger = logmanager.getlogger("helloworld"); public static void main(string[] args) { service s = new service(); string msg = s.getmessage(); logger.info("hello, world!"); } } the compiler complain:
exception in thread "main" java.lang.noclassdeffounderror: org/apache/logging/log4j/logmanager @ execute.entry.<clinit>(entry.java:9) caused by: java.lang.classnotfoundexception: org.apache.logging.log4j.logmanager @ java.net.urlclassloader.findclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ sun.misc.launcher$appclassloader.loadclass(unknown source) @ java.lang.classloader.loadclass(unknown source) ... 1 more update 2
after execute:
>gradle dependencies :dependencies ------------------------------------------------------------ root project ------------------------------------------------------------ archives - configuration archive artifacts. no dependencies compile - compile classpath source set 'main'. \--- org.apache.logging.log4j:log4j-api:2.3 default - configuration default artifacts. \--- org.apache.logging.log4j:log4j-api:2.3 runtime - runtime classpath source set 'main'. \--- org.apache.logging.log4j:log4j-api:2.3 testcompile - compile classpath source set 'test'. +--- org.apache.logging.log4j:log4j-api:2.3 \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3 testruntime - runtime classpath source set 'test'. +--- org.apache.logging.log4j:log4j-api:2.3 \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3 build successful
instead of
import org.apache.log4j.logger; use
import org.apache.logging.log4j.logger; and define instance of logger
private static final logger logger = logmanager.getlogger("helloworld"); see sample http://logging.apache.org/log4j/2.x/manual/api.html
this has nothing todo gradle
Comments
Post a Comment