Create stand-alone jar for appium test scripts - java

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

Related

VSCode Java extension gives error "Cannot find the class file for javax.servlet.http.HttpServletResponse."

I have a java project using gradle as my build system. I am developing in VSCode using the Java extension by Redhat. I am developing on Ubuntu 20.04 with openjdk 11.
When I build the project from the command line using './gradlew assemble', I don't get any build error.
But when I open the project in vscode, I get the following problem in the problems view...
The project was not built since its build path is incomplete. Cannot find the class file for javax.servlet.http.HttpServletResponse. Fix the build path then try building this project
and...
The type javax.servlet.http.HttpServletResponse cannot be resolved. It is indirectly referenced from required .class files
My build.gradle is as follows....
plugins {
id 'com.google.cloud.tools.jib' version '2.4.0'
}
apply plugin: 'java'
// Use maven repository
repositories {
mavenCentral()
google()
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'org.eclipse.jetty:jetty-servlet:9.4.19.v20190610'
implementation 'org.eclipse.jetty:jetty-client:9.4.19.v20190610'
implementation group: 'org.eclipse.jetty', name: 'jetty-util', version: '9.4.29.v20200521'
implementation 'mysql:mysql-connector-java:8.0.16'
implementation 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.16'
implementation 'org.apache.commons:commons-dbcp2:2.7.0'
implementation 'org.jdbi:jdbi3-core:3.10.1'
implementation 'com.google.firebase:firebase-admin:6.11.0'
implementation 'com.google.apis:google-api-services-oauth2:v1-rev155-1.25.0'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
implementation group: 'org.reflections', name: 'reflections', version: '0.9.12'
implementation 'com.github.bhlangonijr:chesslib:1.2.3'
// compile project('chesslib')
//runtime files('../../chesslib/build/libs/chesslib-1.1.12.jar')
}
task runServer(type: JavaExec) {
enableAssertions = true
classpath = sourceSets.main.runtimeClasspath
main = 'ChessServer'
}
task runClient(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'ChessClient'
standardInput = System.in // without this, using Scanner on System.in won't work
}
task runCLITestApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'CLITestApp'
standardInput = System.in
}
// use cloudBuilder.py to invoke this jib task to build the container
// use cloudConfiguration.json to configure things like container version.
jib {
if (project.hasProperty('googleCloudProjectName') && project.hasProperty('imageName')) {
to {
image = "gcr.io/$googleCloudProjectName/$imageName"
}
container {
mainClass = 'ChessServer'
ports = ['8080']
creationTime = "USE_CURRENT_TIMESTAMP"
}
}
}
// make sure the required project properties are set for jib
task jibCheck {
doLast {
if (!project.hasProperty('googleCloudProjectName'))
throw new GradleException("When invoking the jib task, make sure to pass the cli arg -PgoogleCloudProject=[myGoogleCloudProject]\nThis should be the name of the google cloud project which container registry you want to push to.");
if (!project.hasProperty('imageName'))
throw new GradleException("When invoking the jib task, make sure to pass the cli arg -PimageName=[imageName]");
}
}
tasks.jib.dependsOn tasks.jibCheck
// src/main/jib is the default folder that jib looks for files to add to your container
// these files will be placed in the root / dir of the container
task moveFilesForJib(type: Copy) {
from "releaseFiles"
into "src/main/jib"
}
tasks.jib.dependsOn tasks.moveFilesForJib
Add the following to build.gradle. After rebuilding the project, see if the error goes away.
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
The solution was to run
./gradlew wrapper
Then in vscode run Java: Clean Workspace.

Cannot resolve symbol hbase

I am new to HBase, I copied a sample java code from the internet, but I meet an error "Cannot resolve symbol hbase" when building this sample. I use Gradle to build this sample project and Intellij as IDE. The HBase server is a remoter server and I try to write a put sample at my windows laptop to test the HBase, but I am not familiar with HBase and Gradle, could someone suggest what I have missed? Here is my code
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class PutHbaseClient {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("test"));
try {
/*
* Put operations for a single row. To perform a
* Put, instantiate a Put object with the row to insert to and for
* each column to be inserted, execute addcolumn.
*/
Put put1 = new Put(Bytes.toBytes("row1"));
Put put2 = new Put(Bytes.toBytes("row2"));
Put put3 = new Put(Bytes.toBytes("row3"));
put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut1Qual1"));
put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut2Qual1"));
put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut2Qual1"));
put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual2"),
Bytes.toBytes("ValueOneForPut1Qual2"));
put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual2"),
Bytes.toBytes("ValueOneForPut2Qual2"));
put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual2"),
Bytes.toBytes("ValueOneForPut3Qual3"));
table.put(put1);
table.put(put2);
table.put(put3);
} finally {
table.close();
connection.close();
}
}
}
Here is my build.gradle
plugins {
id 'java'
}
group 'gid'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.hadoop', name: 'hadoop-common', version:'2.7.3'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
According to a JAR finder site, the org.apache.hadoop.hbase.TableName class is in the hbase-common JAR file.
That means that the dependency that you have in your build.gradle file should work. However, I don't think that the version number is correct. The most recent 2.x version in Maven Central is 2.2.2. (And 3.0.0-SNAPSHOT is not there ... naturally ... because Maven Central doesn't host SNAPSHOT artifacts!)
However, I recommend that you do what the HBase Documentation says (here):
"For Java applications using Maven, including the hbase-shaded-client module is the recommended dependency when connecting to a cluster."
The corresponding Gradle dependency is:
// https://mvnrepository.com/artifact/org.apache.hbase/hbase-shaded-client
compile group: 'org.apache.hbase', name: 'hbase-shaded-client', version: '2.2.2'
I am not familiar with Gradle, but I expect that there was another error message saying that it couldn't resolve the dependency for hbase-common version 2.7.3.
I think you need something like this in your gradle dependency:
compile 'org.apache.hbase:hbase:3.0.0-SNAPSHOT'

Generated resource output not found in test classpath in gradle build

I am attempting to configure a gradle (version 4.6) build (as a part of a multi-module project) that uses a Java "script" to generate resources into the main sourceSet, which are then referenced in my test configuration. The idea is that I'd ultimately like to create a jar that is simply a resource bundle for inclusion in another module of my build, but I have java files both to generate these resources and to execute verification tests on them before packaging.
I currently have three sourceSets configured: the standard "main" and "test", and a custom sourceSet "generator" which holds resources used as inputs to the generator "script" and the source for the generator script itself. I've registered a main output directory according to this documentation (see "working with generated resources"), pointing to a JavaExec class that runs the generator with the "generator" sourceSet runtime classpath to output resources into the main classpath.
All of this appears to work- I can find the output in the correct directory when running :<module>:build, showing that the script is both running properly and that it is done as a dependency to the main compile task. However, when I try to reference the generated output in the tests with getClass().getClassLoader().getResource("<baseGeneratedOutputDirectory>"), I get a null value back, suggesting that my generated output is not being included in the test runtime classpath. The documentation clearly states that the...
Java plugin will use those dirs in calculating class paths and for jarring the content
...so I'm not sure why my files aren't getting picked up. Below is my abridged build.gradle file. Note that I'm overriding the generator task type in order to set it up with build caching, but I still see this bug with caching turned off.
apply plugin: 'java-library'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
generator {
java.srcDirs = ['src/generator/java']
resources.srcDirs = ['src/generator/resources']
}
main {
java
resources
output.dir("$buildDir/generated-files/main", builtBy: 'generateConfig')
}
test {
java
resources
}
}
dependencies {
api project(':server:server_protobuf_classes');
api project(':common:game-config-util')
api 'com.google.protobuf:protobuf-java:3.5.1'
generatorImplementation project(':common:game-config-util')
generatorImplementation project(':server:server_protobuf_classes');
generatorImplementation group: 'commons-io', name: 'commons-io', version: '2.5'
// ...
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
// ...
}
task generateConfig(type: GenerateConfig) {
outputs.dir("$buildDir/generated-files/main")
classpath sourceSets.generator.runtimeClasspath
main = "com.project.ProtobufConfigurationProcessor"
}
#CacheableTask
class GenerateConfig extends JavaExec {
}
EDIT: Adding the following makes the tests pass, but I'm confused why I'd need to manually configure the test resource directory like this. Shouldn't the test task pick up build output from the main source set by default?
sourceSets {
test {
resources.srcDirs = ["$buildDir/generated-files/main"]
}
}

SpringBoot test module doesn't search log4j2 configuration in the right classpath

I'm having some problems running tests in my SpringBoot project.
The project-structure is the following:
Project Structure Image
I can start the resourceService without issues, but if i even try to run the standard test of SpringBoot projects.....
package com.pcsystem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class ResourceserviceApplicationTests {
#Test
public void contextLoads() {
}
}
the program respond with this error:
Logging system failed to initialize using configuration from 'resourceservice/log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\resourceservice\log4j2.xml (Unable to find the specified classpath)
So i tried to change the application.properties specific property from
logging.config=resourceservice/log4j2.xml
to
logging.config=log4j2.xml
After the change i've noticed that resourceserviceApplication will not start because it can't find the log4j2.xml:
Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml (Unable to find the specified classpath)
I've tried to resolve in many ways and doing a lot of researches but at the moment i'm still stuck here.
Any idea?
ps: it seems that the Authorizationservice module doesn't suffer the same problem, but simply because i haven't set the logging.config property in Authorizationservice's application.properties (there is no need for now)
Thanks in advance and have a great day.
-UPDATE 1-
Configuration file is about all the resourceService module, so i've done as you said Kostiantyn ( thanks for you response ) but the problem still persist.
Actual situation:
Project structure after your reply
Now resourceServiceApplication won't start , saying :
Logging system failed to initialize using configuration from 'log4j2.xml'
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\log4j2.xml
and the contextLoads() method from the test package says:
java.io.FileNotFoundException: C:\java\IntelliJ_projects\baseprojectoauth2\resourceservice\log4j2.xml
Let me show you configuration file:
server.port=8888
logging.config=log4j2.xml
spring.data.mongodb.host=localhost
spring.data.mongodb.database=jogging
#spring.data.mongodb.username=admin
#spring.data.mongodb.password=pass
spring.data.mongodb.port=27017
As requested from user1615664 , below you can see my gradle file
(that's the gradle file about the resourceService module;
AuthorizationService have one specific gradle file and lastly there is
a root gradle file, that i will show to you at the end of this update)
Exscuse me for the lenght, i'm using a large number of libraries here.
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
compile.exclude group:'ch.qos.logback'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude module: "logback-classic"
}
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile group: 'org.apache.tika', name: 'tika', version: '1.16', ext: 'pom'
compile group: 'org.apache.tika', name: 'tika-parsers', version: '1.16'
compileOnly('org.projectlombok:lombok')
compile("org.springframework.boot:spring-boot-starter-actuator")
//compile project(':authorizationservice')
// https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas
compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version: '0.23.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.1.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.8.RELEASE'
// https://mvnrepository.com/artifact/org.apache.axis/axis
compile group: 'org.apache.axis', name: 'axis', version: '1.4'
// https://mvnrepository.com/artifact/axis/axis-jaxrpc
compile group: 'axis', name: 'axis-jaxrpc', version: '1.4'
// https://mvnrepository.com/artifact/commons-discovery/commons-discovery
compile group: 'commons-discovery', name: 'commons-discovery', version: '0.5'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
// https://mvnrepository.com/artifact/javax.xml/jaxrpc-api
compile group: 'javax.xml', name: 'jaxrpc-api', version: '1.1.1'
// https://mvnrepository.com/artifact/org.apache.xmlrpc/xmlrpc
compile group: 'org.apache.xmlrpc', name: 'xmlrpc', version: '3.1.3', ext: 'pom'
// https://mvnrepository.com/artifact/javax.activation/activation
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
// https://mvnrepository.com/artifact/wsdl4j/wsdl4j
compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.3'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
PS: maybe is worthless , but in the root module ( baseProjectOauth2 )
we can admire this root gradle file
group 'com.pcsystem'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
You've missing src/test/resources folder in your project structure, and looks like current location of log4j2.xml in the project root is not under classpath at all
Rely on Gradle project convention and place your config files accordingly, i.e. create either src/test/resources and put file in there, if your logging configuration is test specific:
src
test
java
...
resources
log4j2.xml
Or, if logging configuration inside your log4j2.xml will be used by your application during live run (included into .jar deliverable), move this file to src/main/resources instead
that will bring your logging.config=log4j2.xml working.
Further reading about Gradle project structure & resources folders is here
I feel greedy replying to my own question, but i think i've solved.
I've done these things:
-> created resource folder under test module
src
main
test
java
resources
application.properties
In that properties file i've only put this setup
logging.config=log4j2.xml
And all i have to say now is
"explicative image here"
Simply i have to deep study spring structure (and even gradle one) about auto detect of properties files
You can try to put value classpath:log4j2.xml instead of just log4j2.xml into your application.properties (or application.y[a]ml) file.
All the resources, including this file, should be placed inside the src/main/resources (where application.properties file is also placed). Regardless of a few possible exceptions.
If you want a different configuration of logging for tests, than you should put adapted file inside of tests' resources directory (src/test/resources), but if you want these to be the same, then you can leave it as is (inside src/man/resources only)

Gradle: includeFlat and sourceSets dependecy for per-platform builds

I'm new to Java world and Gradle. I've made a JSerial lib which will support multiple platforms (Android, Linux and Windows).
To be able to choose the platform I'm targetting, I've defined some sourceSets in my JSerial gradle file:
sourceSets {
windows {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
linux {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
dependencies {
linuxCompile 'net.java.dev.jna:jna:4.1.0'
linuxCompile 'net.java.dev.jna:jna-platform:4.1.0'
windowsCompile 'net.java.dev.jna:jna:4.1.0'
windowsCompile 'net.java.dev.jna:jna-platform:4.1.0'
}
The default main sourceSets builds the common interface, etc. Then the windows sourceSet will build windows implementation (and same for Linux and Android).
I create a project which uses this library and depends on it using gradle's includeFlat. Here is the dependency part of my gradle file:
dependencies {
compile project(':JSerial')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
This works. But I would like to depends on the "windows" sourceSet, because this project is a windows application. I tried the following:
dependencies {
compile project(':JSerial').sourceSets.windows.output
testCompile group: 'junit', name: 'junit', version: '4.11'
}
But it doesn't work, I have the following error:
Could not find property 'windows' on SourceSet container.
What's wrong ?
PS: If there is a better way to do what I'm trying without using sourceSets, please tell me !
I finally found a solution which I think is elegant. Instead of using sourceSets I used multi-project. Here is my project:
Serial/
build.gradle
src/main/java/com.package/
SerialPort.java
windows/
build.gradle
src/main/java/com.package/
SerialPortWindows.java
Application/
build.gradle
settings.gradle
In my Application's settings.gradle:
includeFlat 'Serial'
includeFlat 'Serial/windows'
In my Application's build.gradle:
dependencies {
project(':Serial/windows')
}
In my Serial/windows's build.gradle (which requires SerialPort interface to compile):
dependencies {
project(':Serial')
}
Then when I build my application, it requires Serial/windows which requires Serial. I think I will be able to defines multiple build.gradle files for my application (for example one for Linux and one for Windows), whith different dependencies.

Categories

Resources