Skip to content

Latest commit

 

History

History
172 lines (116 loc) · 3.61 KB

GradleQuickSnippets.md

File metadata and controls

172 lines (116 loc) · 3.61 KB

Gradle

Accessing Buildscript ClassPath

 project.buildscript.configurations.classpath

Reference: https://discuss.gradle.org/t/accessing-the-buildscript-classpath/5297/2

Create a project in groovy test

Project project = ProjectBuilder.builder().build()

Write an input stream to file

FileUtils.copyInputStreamToFile(initialStream, targetFile);

Deserialize Generic

ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructParametrizedType(Data.class, Data.class, Parameter.class);
Data<Parameter> dataParam = mapper.readValue(jsonString,type)

Gradle Lines

multiline.eachLine { line, count ->
    if (count == 0) {
        println "line $count: $line"  // Output: line 0: Groovy is closely related to Java,
    }
}

Check task pipeline

./gradlew clean build publish -m

Jackson Ignore Unknown Properties

new ObjectMapper()
  .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

Gradle Unzip File

task unpackFiles(type: Copy) {
    from zipTree("src/resources/thirdPartyResources.zip")
    into "${buildDir}/resources"
}

String Split with All Delimiters

s.trim().split("[\\W]+") 

Groovy ConvertAll

List<Integer> testCaseNumbers = new ArrayList<>();
report.testDetails.each {TestDetail t ->
    t.testCaseNumbers.addAll(t.testCaseNumbers)
}


List<RallyTestCase> response = rallyProvider.getTestCases(testCaseNumbers.collect{x->x.toString()})


//converts [1,2,3] -> ["1","2","3"]

Run Specific Build

gradlew -b <filenam> task

Stop gradle build upon test fail

test.afterTest { TestDescriptor td, TestResult tr ->
    if (tr.resultType == ResultType.FAILURE) {
        throw new Exception("$td failed")
    } 
} 

Setup on System out

    private PrintStream sysOut;
    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();


    @Before
    public void setUpStreams() {
        sysOut = System.out;
        PrintStream contentOutputStream = new PrintStream(outContent)
        System.setOut( new PrintStream( new TeeOutputStream(sysOut, contentOutputStream)));

    }

    @After
    public void revertStreams() {
        System.setOut(sysOut);
    }


    @Test
  
    void test_(){


        ...

        Assert.assertTrue(outContent.toString().contains("<Message>"));


    }

Json Parsing

new JsonSlurper().parseText(jsonAsText)

Pass parameters to Gradle Script

./gradlew myTask -PmyArg=hello

which can be used as

println "$project.myArg"

See https://stackoverflow.com/a/48370451/474377

Convert OutputStream to String

new String( new ByteArrayOutputStream().toByteArray(), codepage );

See https://stackoverflow.com/questions/216894/get-an-outputstream-into-a-string