Spring Rest Doc (Gradle) Snippets disappear upon build - spring-restdocs

Recently switch to Gradle from Maven.
Following this tutorial for continuous REST Doc build with Gradle. https://www.youtube.com/watch?v=k5ncCJBarRI&t=1490s
Snippets are generating just fine when running test. Its when I am trying to generate asciidoc where it seems like the /build directory gets recreated without the snippets. So my generated html always shows
Unresolved directive in index.adoc - include::{snippets}/home-json/curl-request.adoc[]
I am generating the asciidoc by the following command in the terminal
gradle asciidoctor -t
// Continuous build command
// Mentioned around #1:07:40 mark
// https://www.youtube.com/watch?v=k5ncCJBarRI&t=1490s
build.gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.asciidoctor.convert' version '1.5.8'
id 'java'
}
group = 'lab.restdocs'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
repositories {
mavenCentral()
}
ext {
set('snippetsDir', file("build/generated-snippets"))
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'junit:junit:4.12'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}
test {
outputs.dir snippetsDir
useJUnitPlatform()
}
asciidoctor {
inputs.dir snippetsDir
dependsOn test
}
bootJar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
}
MyTest
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
public class HelloControllerTest {
#Autowired
private MockMvc mockMvc;
#Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
#InjectMocks
private HelloController helloController;
#Mock
private HelloService helloService;
#Before
public void setUp() throws Exception {
// create a mock environment of helloController
mockMvc = MockMvcBuilders.standaloneSetup(helloController)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
#Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/hello/string"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("hello there"))
)
.andDo(document("home-string"));
}
I also checked my build.gradle against https://spring.io/guides/gs/testing-restdocs/ and https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/#configuration-uris. I don't know what I am missing...
Thanks in advance.
Edited
Ran command gradle asciidoctor --console=plain
If it makes it easier I greated a Git repo
https://github.com/erich5168/edu.rest-doc
erichuang$ gradle asciidoctor --console=plain
> Task :compileJava
> Task :processResources
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> Task :asciidoctor
asciidoctor: WARNING: api.adoc: line 3: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home/curl-request.adoc
asciidoctor: WARNING: api.adoc: line 5: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home/http-request.adoc
asciidoctor: WARNING: api.adoc: line 7: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home/http-response.adoc
asciidoctor: WARNING: api.adoc: line 20: include file not found: /Users/erichuang/Desktop/JE/00-Development/dev/lab/lab-rest-docs/lab.restdocs-gradlebuild/build/generated-snippets/home-json/http-response.adoc
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 2s
5 actionable tasks: 5 executed
Apples-MBP:lab.restdocs-gradlebuild erichuang$

When run from the command line, you build isn't running any tests. The tests aren't being run as your tests are using JUnit 4 while the test task has been configured to use the JUnit Platform (JUnit 5):
test {
outputs.dir snippetsDir
useJUnitPlatform()
}
You can fix the problem either by updating your tests to use JUnit 5 or by removing useJUnitPlatform() from the test task's configuration so that it uses JUnit 4. The latter is the smaller change and leaves the test task looking like this:
test {
outputs.dir snippetsDir
}

Related

React Native Map Box Execution Failed transform classes with Dex / JarMerging

I've just started using MapBox in react-native but I appear to be getting the following error
* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
Enabling multidex with
multiDexEnabled true
I then get the following
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: okhttp3/internal/ws/RealWebSocket$1.class
Here's the config
build.gradle .../android
allprojects {
repositories {
mavenLocal()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
// Add jitpack repository (added by tipsi-stripe)
maven { url "https://jitpack.io" }
maven { url "https://maven.google.com" }
}
}
build.gradle ../app
dependencies {
compile project(':mapbox-react-native-mapbox-gl')
compile project(':react-native-branch')
compile project(':react-native-camera')
compile project(':tipsi-stripe')
compile project(':react-native-device-info')
compile project(':react-native-vector-icons')
compile project(':react-native-i18n')
compile project(':react-native-geocoder')
compile project(':react-native-fbsdk')
compile(project(':react-native-maps')) {
exclude group: 'com.google.android.gms', module: 'play-services-base'
exclude group: 'com.google.android.gms', module: 'play-services-maps'
}
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:25.0.0"
compile "com.facebook.react:react-native:+" // From node_modules
compile 'com.google.android.gms:play-services-base:11.+'
compile 'com.google.android.gms:play-services-maps:11.+'
}
settings.gradle
include ':mapbox-react-native-mapbox-gl'
project(':mapbox-react-native-mapbox-gl').projectDir = new File(rootProject.projectDir, '../node_modules/#mapbox/react-native-mapbox-gl/android/rctmgl')
include ':react-native-branch'
project(':react-native-branch').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-branch/android')
include ':react-native-camera'
project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android')
include ':tipsi-stripe'
project(':tipsi-stripe').projectDir = new File(rootProject.projectDir, '../node_modules/tipsi-stripe/android')
include ':react-native-device-info'
project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
include ':react-native-i18n'
project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android')
include ':react-native-geocoder'
project(':react-native-geocoder').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-geocoder/android')
include ':react-native-fbsdk'
project(':react-native-fbsdk').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fbsdk/android')
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/lib/android')
include ':app'
MainApplication.java
import android.app.Application;
import android.content.Intent;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.react.ReactApplication;
import com.mapbox.rctmgl.RCTMGLPackage;
import io.branch.rnbranch.RNBranchPackage;
import io.branch.referral.Branch;
import com.lwansbrough.RCTCamera.RCTCameraPackage;
import com.gettipsi.stripe.StripeReactPackage;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import com.oblador.vectoricons.VectorIconsPackage;
import com.i18n.reactnativei18n.ReactNativeI18n;
import com.devfd.RNGeocoder.RNGeocoderPackage;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.airbnb.android.react.maps.MapsPackage;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private static CallbackManager mCallbackManager = CallbackManager.Factory.create();
protected static CallbackManager getCallbackManager() {
return mCallbackManager;
}
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RCTMGLPackage(),
new RNBranchPackage(),
new RCTCameraPackage(),
new StripeReactPackage(),
new RNDeviceInfo(),
new MapsPackage(),
new VectorIconsPackage(),
new ReactNativeI18n(),
new RNGeocoderPackage(),
new FBSDKPackage(mCallbackManager)
);
}
};
#Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
#Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(getApplicationContext());
SoLoader.init(this, /* native exopackage */ false);
// initialize the Branch object
Branch.setPlayStoreReferrerCheckTimeout(0);
Branch.getAutoInstance(this);
}
}
Stacktrace
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lokhttp3/internal/ws/WebSocketReader;
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:63)
at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88)
at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:124)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:80)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:105)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:99)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:99)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: java.lang.RuntimeException: com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lokhttp3/internal/ws/WebSocketReader;
at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:55)
at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:104)
at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:213)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:173)
at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134)
at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:121)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:122)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:111)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
... 27 more
Caused by: com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lokhttp3/internal/ws/WebSocketReader;
at com.android.build.gradle.internal.transforms.DexMergerTransform.transform(DexMergerTransform.java:230)
at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:222)
at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:218)
at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
... 39 more
Caused by: com.android.dex.DexException: Multiple dex files define Lokhttp3/internal/ws/WebSocketReader;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
148 actionable tasks: 4 executed, 144 up-to-date
I'm not sure if this is a matter of my current project config or what not, but i've not seen reports of this so far.
Update with adding in workaround enables app to launch but then another issue arises: NoClassDefFoundError: com.google.android.gms.wallet.MaskedWalletRequest
I've now added in configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' } as suggested by https://github.com/facebook/react-native/issues/12646 which enables the app to build succesfully but then there is an issue of NoClassDefFoundError: com.google.android.gms.wallet.MaskedWalletRequest
Just to clarify moving over to mapbox, I'll no longer need react-native-maps, and the reason for the move over is the greater level of detail i.e. in the buildings.
Thanks, appreciate you're help
Looks like a similar issue was raised against the repo and can probably be resolved if you...
exclude android.arch.core module from the mapbox project
compile 'com.google.android.gms:play-services-base:11.+'
compile 'com.google.android.gms:play-services-maps:11.+`
Version for com.google.android.gms:play-services should be same in build.gradle for app. and and in build.gradle of your packages installed.
-Here 11.+ version refers to any version greater then 11 which currently
available.i would suggest to make this version static and copy that version in build.gradle of your package in node-modules which gives error.
it is necessary to have same com.google.android.gms:play-services version throughout the app
Thankfully I was finally able to get mapbox in on my android version in react-native after resolving issues that popped up along the way: First of all my first attempt was on an older version of react-native 0.44.0, I later upgraded to the latest version of react native
"react-native": "^0.50.3",
and using react
"react": "^16.1.1",
Error
* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
This was the first, the app exceeds the maximum number of methods so we go ahead and enable multiDex in app/gradle
android {
compileSdkVersion 25
defaultConfig {
...
multiDexEnabled true
The next issue was
* What went wrong:
Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: okhttp3/internal/ws/RealWebSocket$1.class
One approach to resolving this was found to use on the older version of react-native but this was not required it seems on the most recent version following the upgrade with react-native-git-upgrade
configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }
Another issue that popped up was
Failed to resolve: android.arch.lifecycle:extension:1.0.0-alpha3
which was resolved by adding maven { url 'https://maven.google.com' } to
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'https://maven.google.com' }
}
}
Then there was an Aapt exception error which was resolved by adding to gradle.properties
android.enableAapt2=false
The app would now build succesfully but on starting irrespective of whether upraded to the latest version or not would show the error
C++ Exception in 'NativeModules': java.lang.NoClassDefFoundError: com.google.android.gms.wallet.MaskedWalletRequest
which was resolved by adding
implementation 'com.google.android.gms:play-services-wallet:11.+'
to our gradle dependencies.
And then the app built and started succesfully with no errors.
I think upgrading to the latest version of react-native, react, helped a great deal with compatability, speed, and performance, as well as by using the latest version of gradle and for example when doing so
configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }
was needed to be added assuming that conflict must have been resolved in a later version.
Anyway, hope this helps someone, and pleased to say I've finally got mapbox in the app.
Thanks to #nitaliano for helping out over at https://github.com/mapbox/react-native-mapbox-gl/issues/785, and providing guidance, a long with the references and help found on https://github.com/tipsi/tipsi-stripe/issues/18 https://github.com/facebook/react-native/issues/12646 https://github.com/mapbox/react-native-mapbox-gl/issues/767 and also the help from Chris Geirman and Rajat Gupta here.
The following references were also helpful
https://facebook.github.io/react-native/docs/upgrading.html
https://reactjs.org/blog/2017/04/07/react-v15.5.0.html
https://reactjs.org/blog/2017/09/26/react-v16.0.html
https://github.com/mlaursen/react-md/issues/325
https://www.npmjs.com/package/prop-types
https://github.com/mapbox/react-native-mapbox-gl/blob/master/example/src/App.js
https://github.com/mapbox/react-native-mapbox-gl/blob/master/example/src/components/ShowMap.js
Mapbox: Cant add LocationLayer plugin in gradle
https://github.com/tipsi/tipsi-stripe/issues/18
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html
Thanks again 👍

The projectDir is invisible from the task class in Gradle script

It is an example of a build.gradle set on an empty freshly created Gradle/Groovy project on IntellJ. I use projectDir here in two places: in task definition and in task class definition. IntelliJ shows me the first use as incorrect - cannot resolve symbol. And it sees the second use as a correct one.
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'groovy'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
task ProjectDirTest(type: ProjectDirTestClass){
println " -------------- $projectDir from task"
}
class ProjectDirTestClass extends DefaultTask {
#TaskAction
def greet() {
println " -------------- $projectDir from class"
}
}
configure(ProjectDirTest) {
group = 'ProjectDirTest'
description = 'ProjectDirTest'
}
But if I run the task, the first println works OK, with correct output:
12:55:41: Executing external task 'ProjectDirTest'...
-------------- C:\Users\543829657\testWorkspace from task
:ProjectDirTest FAILED
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\543829657\testWorkspace\build.gradle' line: 28
* What went wrong:
Execution failed for task ':ProjectDirTest'.
> Could not get unknown property 'projectDir' for task ':ProjectDirTest' of type ProjectDirTestClass.
But, as you see, at the second println there is a problem - Gradle does not see the projectDir variable.
I am already accustomed to incorrect IntelliJ marking errors. But how can I make the task class to see the the projectDir variable in runtime?
In the real task class I also cannot use the exec() command - Gradle does not see it, too.
I have found a temporary solution, to pass the project variable into the class and call all project variables with it:
task ProjectDirTest(type: ProjectDirTestClass){
println " -------------- $projectDir from task"
projectLoc = project
}
class ProjectDirTestClass extends DefaultTask {
Project projectLoc
#TaskAction
def greet() {
println " -------------- ${projectLoc.projectDir} from class"
}
}
The negative side is that I have to pass the same project for every task using that task class. It is not a good style.

Gradle execute Groovy script in GroovyShell with Eclipse Luna

I am getting a ClassNotFoundException: org.apache.ivy.core.report.ResolveReport when executing a gradle task, which uses Grapes to resolve dependencies.
I am using Eclipse Luna 4.4.0 with a Gradle/Groovy Project having this build.gradle:
apply plugin: 'groovy'
apply plugin:'application'
mainClassName = "de.my.app.package.Main"
version = 0.5
repositories { mavenCentral() }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.3'
compile group: 'org.apache.ivy', name:'ivy', version:'2.2.0'
compile 'commons-io:commons-io:2.4'
compile 'commons-codec:commons-codec:1.2'
}
task myTask << {
def groovyShell = new GroovyShell()
groovyShell.run(file('/src/scripts/groovy/de/my/app/package/scripts/SomeScript.groovy'))
}
classes.finalizedBy(myTask)
My Java Build Path inside Project->Properties looks like this:
This is SomeScript.groovy inside the Folder /src/scripts/groovy/de/my/app/package/scripts:
package de.my.app.package.scripts
#Grapes(
#Grab(group='org.eclipse.birt.runtime.3_7_1', module='org.apache.commons.codec', version='1.3.0')
)
#Grapes(
#Grab(group='commons-io', module='commons-io', version='2.4')
)
import org.apache.commons.codec.binary.Hex
println Hex.toString()
Weird thing is that executing SomeScript.groovy from cmd with groovy SomeScript.groovy does not give the error. So i am guessing it is some Eclipse config I have missed.
How can SomeScript.groovy be executed by the Gradle run from the build.gradle without causing a ClassNotFoundException: org.apache.ivy.core.report.ResolveReport?
I have found a solution for my problem. I needed this build.gradle file:
apply plugin: 'groovy'
apply plugin:'application'
mainClassName = "de.my.app.package.Main"
version = 0.5
repositories { mavenCentral() }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.3'
compile group: 'org.apache.ivy', name:'ivy', version:'2.2.0'
compile 'commons-io:commons-io:2.4'
compile 'commons-codec:commons-codec:1.2'
}
task myTask (type: Exec) {
def groovyHome = System.getenv("GROOVY_HOME")
def someScriptPath= new String(project.projectDir.toString()).toString() + "\\src\\scripts\\groovy\\de\\my\\app\\package\\main\\SomeScript.groovy"
commandLine "${groovyHome}\\bin\\groovy.bat", someScriptPath
}
classes.finalizedBy(myTask)
So I abandoned the approach of using the class GroovyShell, because I could not configure the classPath for it correctly.
My Script executes now before every run. Problem is solved.

Can't run Scalatest with Gradle

task scalaTest(dependsOn: testClasses) << {
description = 'Runs Scalatest suite'
ant.taskdef(name: 'scalatest',
classname: 'org.scalatest.tools.ScalaTestAntTask',
classpath: sourceSets.test.runtimeClasspath.asPath
)
ant.scalatest(runpath: sourceSets.test.output.classesDir,
haltonfailure: 'true', fork: 'false') {
reporter(type: 'stdout')
}
}
I run gradle scalaTest and I get:
* What went wrong:
Execution failed for task ':scalaTest'.
> java.lang.NoClassDefFoundError: scala/reflect/ClassManifest$
I am using Scala 2.10.2 and Gradle 1.7
dependencies {
compile 'org.scala-lang:scala-library:2.10.2'
testCompile 'org.scalatest:scalatest:1.3'
testCompile 'org.scalamock:scalamock_2.10:3.0.1'
}
What's wrong??
I do not know how to solve this one, but I can offer you a workaround. Annotate your test classes with #RunWith(classOf[JUnitRunner]), like this:
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
#RunWith(classOf[JUnitRunner])
class MyTest extends FunSpec{
}
and then, gradle test should work.
Edit:
My dependencies:
compile "org.scala-lang:scala-library:2.10.1"
testCompile "org.scalatest:scalatest_2.10:1.9.1"
You can put the following in your build.gradle:
task spec(dependsOn: ['testClasses'], type: JavaExec) {
main = 'org.scalatest.tools.Runner'
args = ['-R', 'build/classes/scala/test', '-o']
classpath = sourceSets.test.runtimeClasspath
}
Note: The path my be different depending on the gradle version as pointed out in the comments by #MikeRylander. Before gradle 4 it used to be 'build/classes/test'.
Then just run gradle spec to execute your tests.
I named the task spec because there already is a test task. I don't know if you can override the default test task.
You can look up the available options here.
This response may be a bit late. But for one using scala with gradle (5.x), the following works.
Add the following plugin to gradle.
plugins {
id "com.github.maiflai.scalatest" version "0.25"
}
To run the code
> gradle test
As a bonus the test results from the above plugin would also be reported better than the default report.
rarry is correct. And even better, you can annotate a base test class with #RunWith(classOf[JUnitRunner]) once to cause all of your ScalaTest tests to run with the JUnit runner (assuming they extend the base class), e.g.:
import org.junit.runner.RunWith
import org.scalatest._
import org.scalatest.junit.JUnitRunner
#RunWith(classOf[JUnitRunner])
abstract class UnitSpec extends FlatSpec with Matchers
As of Gradle 3.2.1, following root build.gradle runs both JUnit and ScalaTest code. This code is for multi-project build, enabled via settings.gradle, that's why subprojects used.
Plus, it's on purpose uses explicit Gradle API, to minimize Gradle DSL magic.
description = 'root project'
def enableScalaTest(Project project) {
Task scalaTest = project.task(
[
'dependsOn': 'testClasses',
'description': 'Runs ScalaTest tests in the project'
],
'scalaTest',
{
ext.inputDir = project.tasks.getByName('compileTestScala').destinationDir
inputs.dir(ext.inputDir)
}
)
scalaTest.doLast({
ant.taskdef(name: 'scalatest',
classname: 'org.scalatest.tools.ScalaTestAntTask',
classpath: project.sourceSets.test.runtimeClasspath.asPath)
ant.scalatest(runpath: ext.inputDir,
fork: 'false',
haltonfailure: 'true')
{ reporter(type: 'stdout') }
})
project.getTasks().getByName('build').dependsOn(scalaTest)
}
subprojects {
apply plugin: 'scala'
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.12.1'
testCompile 'junit:junit:4.12'
testCompile 'org.scalatest:scalatest_2.12:3.0.1'
}
enableScalaTest(delegate)
}

how to copy the dependencies libraries JARs in gradle

I got a runnable jar with this build.gradle
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes("Main-Class" : "com.test.HelloWorld")
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
but it run failed, because the dependencies jars can't find.
and then I add this code:
task copyToLib(type: Copy) {
into "$buildDir/output/libs"
from configurations.runtime
}
but nothing change. I can't find the folder output/libs.
how can I copy the dependencies libs jars to a specified folder or path?
Add:
build.dependsOn(copyToLib)
When gradle build runs, Gradle builds tasks and whatever tasks depend on it (declared by dependsOn). Without setting build.dependsOn(copyToLib), Gradle will not associate the copy task with the build task.
So:
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes('Main-Class': 'com.test.HelloWorld')
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
task copyToLib(type: Copy) {
into "${buildDir}/output/libs"
from configurations.runtime
}
build.dependsOn(copyToLib)
I find the application plugin way too cumbersome and too verbose in its output. Here's how I finally got a setup I was happy with, i.e., create a distribution zip file with dependency jars in subdirectory /lib and add all dependencies to Class-Path entry in the manifest file:
apply plugin: 'java'
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
}
// Task "distZip" added by plugin "java-library-distribution":
distZip.shouldRunAfter(build)
jar {
// Keep jar clean:
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
manifest {
attributes 'Main-Class': 'com.somepackage.MainClass',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
// How-to add class path:
// http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
// https://gist.github.com/simon04/6865179
}
Hosted as a gist here.
The result can be found in build/distributions and the unzipped contents look like this:
lib/commons-lang3-3.3.2.jar
MyJarFile.jar
Contents of MyJarFile.jar#META-INF/MANIFEST.mf:
Manifest-Version: 1.0
Main-Class: com.somepackage.MainClass
Class-Path: lib/commons-lang3-3.3.2.jar
Since Gradle 6.0 it is:
tasks {
val deps by registering(Copy::class) {
from(configurations.runtimeClasspath)
into("build/deps")
}
}
The problem with all the previous answers is that they only collect dependencies from one configuration. To get ALL of the dependencies, you should use this:
task saveDependencies(type: Copy){
configurations.each {
if (it.isCanBeResolved())
from it into "gradle_dependencies"
}
from buildscript.configurations.classpath into "gradle_dependencies"
}
The application plugin requires you to set the main class name like this:
mainClassName = "com.test.HelloWorld"
You will need to add that to your build script. Keep in mind that if you try to run your application with the java command you will also need to set the classpath with -cp.
The application plugin simplifies this process by providing the task distZip. If you run that task you a full distribution is created for you under build/distributions. The distribution contains start scripts and all dependencies. The generated start scripts already set the classpath for you so you don't have to deal with it anymore.
The java plugin can pack a jar with dependencies and there's no need for the application plugin. A task like the following would do:
task buildWithDeps(type: Jar) {
manifest {
attributes "Main-Class": "com.test.HelloWorld"
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
As of at least Gradle 5.6.4 you'll want to do something closer to this.
dependencies {
implementation 'my.group1:my-module1:0.0.1'
implementation 'my.group2:my-module2:0.0.1'
}
jar {
from {
configurations.compileClasspath.filter { it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
For Gradle 7.4 with Groovy:
configurations {
externalLib.extendsFrom(implementation)
}
task copyLibs(type: Copy){
from configurations.externalLib{
into '<dest-dir-name>'
exclude('<if any jars need to be excluded>')
}
}