I am using java 1.8 for Gradle SpringBoot restAPI.
I haven't used spring-boot-starter-security and API works accordingly but after deploying the same code on heroku the API asks for the signup.
I tried disabling it via following way,
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
#SpringBootApplication (exclude = { SecurityAutoConfiguration.class })
public class LawCaseApplication {
public static void main(String[] args) {
SpringApplication.run(LawCaseApplication.class, args);
}
}
But still after re-deploying the screen appeared.
Then I tried giving it my own user name and password by writing the following in the application.properties as I wasn't getting any randomly generated password in the logs too,
spring.security.user.name="user"
spring.security.user.password="user"
And I am still getting this screen.
However, the dependencies I used are,
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.20'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation "org.jsoup:jsoup:1.13.1"
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
}
I repeat that there is no security when run on local host but on the heroku deployed version.
Actually, there is a most important step before you redeploy each time.
build the jar/war file so that it gets updated by the recent changes you made...
The steps you used are working on localhost proving them to be correct...
Try building the jar/war file then check if it works or not...
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
I am finally learning Java 9+ modules, and I want to migrate some of my old 1.8 projects.
I started simple and created a standard IntelliJ project, and I created this:
src
|__main
|__java
|__myfirst.module
|__Util.java
|__module-info.java
Here is module-info.java
module myfirst.module {
requires org.apache.commons.lang3;
exports myfirst.module;
}
and here is Util.java
package myfirst.module;
import org.apache.commons.lang3.math.NumberUtils;
public class Util {
public static void main(String[] args) {
System.out.println("Bonjour"+ NumberUtils.isCreatable("Bonjoru"));
}
}
And the build.gradle
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.10'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
But when running gradle's jar task, I get the following error:
src/main/java/module-info.java:2: error: module not found: org.apache.commons.lang3
requires org.apache.commons.lang3;
Can someone explain me why?
Thanks.
In fact, it seems like it is not yet possible, according to Gradle:
Gradle doc
I guess I'll try to do this with Maven instead.
I am following the spring boot documentation https://spring.io/guides/gs/spring-boot/
but after I did the same as in Add Unit Tests step, even though I have the same gradle file, the project will not build because the import failed.
gs-spring-boot\complete\src\main\java\com\example\springboot\HelloControllerTest.java:3: error: package org.hamcrest does not exist
import static org.hamcrest.Matchers.equalTo;
^
error: package org.assertj.core.api does not exist
import static org.assertj.core.api.Assertions.*;
My gradle file content
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
and entire project setup same is given in https://github.com/spring-guides/gs-spring-boot/archive/master.zip
What went wrong here? I have the java SDK setup and I am able to run the http server right before this step. I am on windows 10 using IntelliJ and its built in gradle.
Not sure if this is still a useful answer, but I also ran into this issue.
The problem seems to be that your code is in the 'main' directory instead of the 'test' directory. Moving the test files into the test directory fixed this problem for me.
You're missing Hamcrest:
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest
testImplementation "org.hamcrest:hamcrest:2.2"
I'm writing a Java program - a plain command line program, not Android or anything like that - using Gradle, and trying to include Apache Commons IO. Per https://mvnrepository.com/artifact/commons-io/commons-io/2.6 I ended up with build.gradle like this:
apply plugin: 'application'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/commons-io/commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
}
mainClassName = 'Main'
Gradle seems to download the package happily enough, but import statements referring to apache or commons get a not found error; this is true even when I run gradle build from the command line, omitting any IDE. What am I missing? (Previous similar discussions have been for Android or Eclipse projects; the instructions for those haven't worked here.)
I don't see any problems with your Gradle script, so at first glance, I'm thinking of two possible explanations:
you import a class that cannot be found in the artifact (e.g. a previous version of commons-io deprecated it and now they removed it)
your project's structure is set up incorrectly
I copied the exact contents of your script in a build.gradle file, created a src/main/java directory structure in the same directory, and wrote a small application Main.java under that directory with the following:
import org.apache.commons.io.IOCase;
public class Main {
public static void main(String[] args) {
System.out.println(IOCase.SENSITIVE.checkEndsWith("abcd1234", "1234"));
}
}
The code compiles successfully, so I need more context to troubleshoot your problem.