Gradle fails to load org.apache.logging.log4j dependencies properly - java

I am having an issue where gradle is somehow failing to make the org.apache.logging.log4j dependencies to the application.
Below is my gradle.build file,
plugins {
id 'java'
id 'war'
}
group 'com.hf'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
compileJava {
options.compilerArgs += ["-proc:none"]
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.11.0'
providedCompile 'org.hibernate:hibernate-core:5.3.7.Final'
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
It's seemingly straightforward. I have -proc:none because there seemed to be an issue with some deprecation otherwise. Regardless of whether proc:none is present in the gradle file, the issue persist. The output of gradle.build is as follows...
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :war UP-TO-DATE
> Task :assemble UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build UP-TO-DATE
BUILD SUCCESSFUL in 6s
3 actionable tasks: 3 up-to-date
The dependency files are also present in the cache directory,
C:\Users\MY_USER_NAME.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j
containing the following dirs:
log4j log4j-api log4j-bom log4j-core log4j-jcl log4j-to-slf4j
all of which have the respective log4j jars nested inside them.
The problem is that eclipse does not recognize the log4j imports,
enter image description here
As a result I can't compile my simple class,
package com.hf.config;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* The Config class reduces the amount of magic strings in the application via the
* getProperty method. It has a static string CONFIG_FILE indicating the path to the config file
* which must be set manually
* #version 1.0
*/
public class AppConfig
{
static Logger log = LogManager.getLogger(AppConfig.class.getName());
private static final String CONFIG_FILE = "./app_config.txt";
/**
* Gets a property from the CONFIG_FILE corresponding to propertyName
* #param propertyName
* #return a string value corresponding to propertyName
*/
public static String getProperty(String propertyName)
{
Properties prop = new Properties();
try(InputStream inStream = AppConfig.class.getClassLoader().getResourceAsStream(CONFIG_FILE))
{
if(inStream == null)
{
log.error("config error: did not find file '" + CONFIG_FILE + "', failed to load property " + propertyName);
return null;
}
prop.load(inStream);
return prop.getProperty(propertyName);
} catch(IOException e)
{ e.printStackTrace();
}
log.error("config error: received property request '" + propertyName + "' which does not have a corresponding value in " + CONFIG_FILE);
return propertyName;
}
}
I am very new to gradle, it's something we use at work so I am trying to incorporate it as a dependency manager for my personal project to get more familiar with it. Sadly I've spent two days now trying to find a solution to this issue and am hoping someone more knowledgeable or experienced can help me resolve this. Thanks for your help in adance.

I resolved my problem by removing the project from eclipse and then re-importing it. That seems to have refreshed whatever settings were wrong and everything is working fine.
What I originally did in my frustration was install the IntelliJ IDE. When I imported my project into IntelliJ everything was working okay >___<.
That gave me the idea to remove the project from eclipse and re-import it which also fixed the problem in that IDE as well. I can only guess that gradle or eclipse were not updating the state of the project on some level.
While I'm glad to be moving passed this, I'm also rather annoyed. Hopefully this will be of some help to those running into similar issues in the future.

Related

Create stand-alone jar for appium test scripts

I would like to create a stand-alone (thin jar) jar without dependencies for Appium test scripts.
I have a Runner class
import org.junit.runner.JUnitCore;
import java.net.MalformedURLException;
public class Runner {
public static void main(String[] args) throws MalformedURLException {
try{
JUnitCore.runClasses(Calculator.class);
}finally {
}
}
}
and
I have a Calculator test class
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Calculator {
// WebDriver driver;
public AndroidDriver<MobileElement> driver;
#Before
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("udid", "ZH33L2Z6KL"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "6.0.1");
caps.setCapability("automationName", "uiautomator2");
caps.setCapability("skipUnlock","true");
caps.setCapability("appPackage", "com.google.android.calculator");
caps.setCapability("appActivity", "com.android.calculator2.Calculator");
caps.setCapability("noReset","true");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
}
#Test
public void testCal() throws Exception {
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.id("digit_2"));
two.click();
WebElement plus=driver.findElement(By.id("op_add"));
plus.click();
WebElement four=driver.findElement(By.id("digit_4"));
four.click();
WebElement equalTo=driver.findElement(By.id("eq"));
equalTo.click();
//locate the edit box of the calculator by using By.tagName()
WebElement results=driver.findElement(By.id("result_final"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6";
}
#After
public void teardown(){
//close the app
driver.quit();
}
}
I have gone through one article about ThinJar and hollowJar.
https://dzone.com/articles/the-skinny-on-fat-thin-hollow-and-uber
Questions
How to add Gradle task (in intellij)to build thin jar as per the article?
How to add Gradle task to build 'Hollow' jar as per the article?
If I build a 'fat' jar my jar size is 18mb. How to build skinny or thin jar with less size, and keep dependencies separately?
How to run the created 'skinny' or 'thin' jar in different PC?
The terminology used in your link is a bit strange. With gradle, the "skinny" jar is always built. It is the default artifact, check the build/libs folder. If you apply the application plugin, there is a distribution zip built as well under build/distribution which is pretty much the fat jar (it is a zip of all relevant jars). But by definition you cannot build a fat jar into a smaller size, and you cannot simply run the "skinny" or "thin" jar on the target host.
Running your application always requires just three things:
The compiled artifact of your code - a bunch of .class files corresponding to, and only to, the code you write, usually packaged in a jar format. This is the skinny jar in that terminology. This is also the commonest artifact produced out of a build (any build system, maven, gradle etc) if you don't do anything special.
The library dependencies - all 3rd party jars
The runtime - this usually refers to Java itself.
All of the above need to present on the host where you are about to run your application. Now, what gets a bit complicated is the stuff you actually need to ship to that host (this is called deployment):
Obviously you will need No. 1 shipped to the host
Usually you would expect/assume No. 3 is pre-installed on the host
What about No.2 the 3rd party dependencies? The answer is it depends.
If (some of) these dependencies can be pre-installed on the target
host, you don't need to ship them. In this case usually people would
just call these dependencies as also the "runtime". For example,
Maven is a runtime, so is Gradle. These are, in themselves, Java
libraries to you when you are writing a Maven/Gradle plugin. You
would normally expect people using your code to have maven/gradle
installed already. They run your code through maven/gradle, and
maven/gradle will provide the dependencies your code requires
when running it. This is why in maven this kind of dependencies is
called "Provided". Maven has a dedicated dependency scope for it.
If any of your dependencies is not provided on the target host, you
need to ship it, period.
In Gradle, if you apply the application plugin (which will automatically apply the distribution plugin), you can have both your artifact and your dependencies (exclude java runtime) in a single zip - this is called a distribution.
plugins {
id 'java'
id 'application'
}
Once you build, you will find a zip file under build/distributions. There are two folders if you unzip the file: bin and lib. Within the lib folder sits your jar, and all your dependencies jars. This technically is not a fat jar, because it is not a single jar. But jar-or-not is just a format. Eventually what you are after is just to get your code and dependencies across to the target host. The distribution zip does not mess around with jar because jar-merging is not as simple as folder merges. Instead distribution zip expects you to unzip on the target host and invoke the script under bin folder to start the application.
The following article answers these questions:
Java Build Automation Part 2: Create executable jar using Gradle
https://vocon-it.com/2016/11/15/how-to-build-a-lean-jar-file-with-gradle/
The corresponding sample code is available under:
https://github.com/oveits/gradle-tutorial-build-executable-jar/releases/tag/v1.0
The final build.gradle file is given below:
group 'io.cloudgrey.appiumpro'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
maven { url "https://jitpack.io" }
mavenCentral()
maven { url "https://repo.maven.apache.org/maven2" }
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'log4j', name: 'log4j', version:'1.2.17'
testCompile group: 'io.appium', name: 'java-client', version: '7.3.0'
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.13.0'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
testCompile group: 'com.eclipsesource.minimal-json', name: 'minimal-json', version: '0.9.5'
testCompile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.9'
testCompile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.10'
testCompile group: 'me.xdrop', name: 'fuzzywuzzy', version: '1.2.0'
testCompile group: 'io.appium', name: 'mitmproxy-java', version: '1.6.1'
testCompile group: 'com.applitools', name: 'eyes-appium-java4', version: '4.2.1'
testCompile group: 'com.github.testdotai', name: 'classifier-client-java', version: '1.0.0'
testCompile group: 'com.google.guava', name: 'guava', version: '28.1-jre'
}
test {
outputs.upToDateWhen {false}
useJUnit()
testLogging {
exceptionFormat = 'full'
showStandardStreams = true
}
maxParallelForks = 3
forkEvery = 1
}
task copyJarsToLib (type: Copy) {
def toDir = "build/libs/dependency-jars"
// create directories, if not already done:
file(toDir).mkdirs()
// copy jars to lib folder:
from configurations.compile
into toDir
}
task marshallClasspathDeps(type: Copy) {
from sourceSets.test.runtimeClasspath
// if you need this from the dependencies in the build.gradle then it should be :
// from sourceSets.main.runtimeClasspath
include '**/*.class'
include '**/*.jar'
exclude { FileTreeElement details ->
details.file.name.contains('Edition') || details.file.name.contains('kotlin')
}
into 'build/libs/dependency-jars'
}
build.dependsOn(marshallClasspathDeps)
task buildJar(dependsOn:classes,type: Jar) {
// exclude log properties (recommended)
exclude ("log4j.properties")
// make jar executable: see http://stackoverflow.com/questions/21721119/creating-runnable-jar-with-gradle
manifest {
attributes (
'Main-Class': 'com.example.appium.Runner',
// add classpath to Manifest; see http://stackoverflow.com/questions/30087427/add-classpath-in-manifest-file-of-jar-in-gradle
"Class-Path": '. dependency-jars/' + sourceSets.test.runtimeClasspath.collect { it.getName() }.join(' dependency-jars/')
)
}
baseName = "AppiumTests"
from sourceSets.test.output
include 'com/example/appium/*.class'
}
// always call copyJarsToLib when building jars:
jar.dependsOn copyJarsToLib
build.dependsOn(buildJar)
// END AUTOMATICALLY INSERTED
The test cases can be executed using:
java -jar -Dlog4j.configuration=file:%cd%\log4j.properties build\libs\AppiumTests.jar

Gradle - Create custom Groovy library with Java dependencies (apache commons)

I'm trying to create a groovy library (JAR) using java dependencies with no success. I managed to use simple methods, but when my code use third party libraries it won't find the related class.
Gradle version: 5.2
Groovy 1.8.9
Project Structure:
build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Groovy project to get you started.
* For more details take a look at the Groovy Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/5.2/userguide/tutorial_groovy_projects.html
*/
plugins {
// Apply the groovy plugin to add support for Groovy
id 'groovy'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// Use the latest Groovy version for building this library
implementation 'org.codehaus.groovy:groovy-all:1.8.8'
compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
// https://mvnrepository.com/artifact/org.spockframework/spock-core
testCompile group: 'org.spockframework', name: 'spock-core', version: '1.1-groovy-2.4'
}
settings.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user manual at https://docs.gradle.org/5.2/userguide/multi_project_builds.html
*/
rootProject.name = 'com.itau.plugins'
MyClassWithDependencies:
package com.itau.plugins
import org.apache.commons.io.FileUtils
class MyClassWithDependencies {
def copy(source, destination) {
FileUtils.copyFile(new File(source), new File(destination))
}
def copyDir(source, destination) {
FileUtils.copyDirectory(new File(source), new File(destination))
}
}
The error is pretty clear:
When i try to import it throws Groovy:unable to resolve class org.apache.commons.io.FileUtils
And, when i run gradlew build --no-build-cache the output is
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 up-to-date
and my gradle dependencies are (i guess a picture is the best option here):
Any help would be really appreciated. Thanks in advance.
P.S. I managed to create a Fat Jar but that's not what i need.

Can not publish Java project to Maven via BinTray with Gradle

I have a simple project I'm trying to publish to Maven via BinTray, but have been getting an error.
I have followed a guide to publish to bintray, and appear to have BinTray all set up, as well as access to Sonatype. Signing/etc, all appears to be good.
When I run "./gradlew bintrayUpload" I get an error, but the artifacts do show up on bintray. However I get various POM errors.
adams-MBP:UsedUtil adamhammer2$ ./gradlew clean install
:clean
:compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.7
Note: /Users/adamhammer2/git/UsedUtil/src/main/java/com/mysaasa/usedutil/CallKeyGenerator.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
:processResources UP-TO-DATE
:classes
:jar
:javadoc
:javadocJar
:sourcesJar
:install
BUILD SUCCESSFUL
Total time: 7.233 secs
adams-MBP:UsedUtil adamhammer2$ ./gradlew bintrayUpload
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:javadoc UP-TO-DATE
:javadocJar UP-TO-DATE
:sourcesJar UP-TO-DATE
:install
:bintrayUpload FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bintrayUpload'.
> Could not upload to 'https://api.bintray.com/content/adamhammer/maven/used-util/0.9.1/com/mysaasa/used_util/UsedUtil/0.9.1/UsedUtil-0.9.1.pom': HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path 'com/mysaasa/used_util/UsedUtil/0.9.1/UsedUtil-0.9.1.pom']
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 12.923 secs
Github project is https://github.com/ahammer/UsedUtil
build.gradle file is
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
apply plugin: 'java'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
ext {
bintrayRepo = 'maven'
bintrayName = 'used-util'
publishedGroupId = 'com.mysaasa.used_util'
libraryName = 'UsedUtil'
artifact = 'usedutil'
libraryDescription = 'A Library for tracking usage in java projects'
siteUrl = 'http://ahammer.github.io/UsedUtil'
gitUrl = 'https://github.com/ahammer/UsedUtil.git'
libraryVersion = '0.9.1'
developerId = 'adamhammer'
developerName = 'Adam Hammer'
developerEmail = 'adamhammer2#gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
After publishing I get the error, however in BinTray it registers the upload. When I click add to JCenter however I get another error "- Add a POM file to the latest version of your package."
Instead of using the 3rd party script and out of date plugin, I followed the guide
here https://github.com/bintray/gradle-bintray-plugin
This generated a proper pom file and published to bintray and made my package compatible with jcenter.
For a working build.gradle, you can look at my github for the working version.

Is it mandatory to define the java plugin to download the depdendency in gradle script?

I am writing the gradle script where I need to download the multiple jar files from artifactory I am using the apply plugin 'java' in gradle script and able to fetch those jar files from artifactory easily but if I apply this plugin it run the jar task automatically and creates the jar which I don't want.Is there a way to download the jar files from artifactory with/without using the java plugin so that jar task couldn't trigger.
apply plugin 'java'
apply plugin: 'base'
//-- set the group for publishing
group = 'com.abcdef.covery'
/**
* Initializing GAVC settings
*/
def buildProperties = new Properties()
file("version.properties").withInputStream {
stream -> buildProperties.load(stream)
}
//add the jenkins build version to the version
def env = System.getenv()
if (env["BUILD_NUMBER"]) buildProperties.coveryadBuildVersion += "_${env["BUILD_NUMBER"]}"
version = buildProperties.scoveryadBuildVersion
println "${version}"
//name is set in the settings.gradle file
group = "com.abcdef.discovery"
version = buildProperties.coveryadBuildVersion
println "Building ${project.group}:${project.name}"
repositories {
maven {
url "http://art.tsh.tho.com:90000/artifactory/services"
}
}
dependencies {
runtime "covery.services:AddService:1.0#jar"
runtime "covery.services:AddService:1.1#jar"
runtime "covery.services:Services:1.0#jar"
runtime "covery.services:Services:1.1#jar"
}
task copyDeps(type: Copy) {
from configurations.runtime
into 'services/discovery/services/'
}
output:Below shows few tasks which are running while using the java plugin in the script and if I don't use then it doesn't download the jar files from artifactory.
16:28:32 Building com.abcdefgh.discovery:cdad
16:28:33 [buildinfo] Properties file found at 'C:\Windows\TEMP\buildInfo429617857022686528.properties'
16:28:35 :copyDeps UP-TO-DATE
16:28:36 :deletebuild
16:28:37 :buildreportZip
16:28:38 :deleteGraphicsAssets
16:28:47 :unzip
16:28:47 :compileJava UP-TO-DATE
16:28:47 :processResources UP-TO-DATE
16:28:47 :classes UP-TO-DATE
16:28:47 :jar
16:28:47 :artifactoryPublish
16:28:47 Deploying artifact: http://localhost:8081/artifactory/libs-release-local/com/abcdefg/covery/cdad/03_00_00_183/cdad-03_00_00_183.zip
16:28:53 Deploying artifact: http://localhost:8081/artifactory/libs-release-local/com/abcdefl/covery/cdad/03_00_00_183/cdad-03_00_00_183.jar
You do not need to add the java plugin to download dependencies. If you do not have any java source files I would recommend that you use the baseplugin instead:
apply plugin: 'base'
repositories {
mavenCentral()
}
configurations {
nameOfMyConfiguration
}
dependencies {
nameOfMyConfiguration 'org.scala-lang:scala-library:2.10.4'
}
task zipMyStuff(type: Zip) {
from configurations.nameOfMyConfiguration
}

Followup to Gradle 1.3: build.gradle not building classes

I am actually using the newly released Gradle 2, but having the same issues as described in the previous post.
I am also a newb trying to follow the example given in the Spring guide (http://spring.io/guides/gs/gradle/#scratch) but after my first compile, there were no classes.
I have tried various configurations of tree structure including adding the structure and code suggested in the above thread:
"I guess the source file path is src/org/gradle/example/simple/HelloWorld.java. (The diagram shows something different.) That doesn't match Gradle's default, so you'll have to reconfigure it: sourceSets.main.java.srcDirs = ["src"] – Peter Niederwieser Dec 7 '12 at 1:23 "
adding the line: sourceSets.main.java.srcDirs = ["src"] allows the code to compile, however, I still have no classes.
Here is the successful build.
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
BUILD SUCCESSFUL
Total time: 4.468 secs
Here is the build file:
apply plugin: 'java'
sourceSets.main.java.srcDirs=["src"]
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile "joda-time:joda-time:2.2"
}
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
task wrapper(type:Wrapper) {
gradleVersion = '1.11'
}
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
Where are my classes? Please help.
After I got stuck with the same problem, I hacked around for a bit before I understood the reason for this behavior.
My project structure was like so
MyProject
- src
- main
- java
- hello
HelloWorld.java
build.gradle
The problem was that the build.gradle is supposed to be under the Project-Root folder i.e. MyProject and not under the hello folder !!!
Changed it so that my Project structure looks like below, ran the gradle build and saw that classes folder was created:
MyProject
- src
- main
- java
- hello
HelloWorld.java
build.gradle
When you think about it, the build.gradle is used to build the complete project and not just the classes within one folder and should rightfully sit under the project-root folder.

Categories

Resources