DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Introduce a New API Quickly Using Spring Boot and Gradle
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Building AI Applications With Java and Gradle
  • Understanding Dependencies...Visually!

Trending

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Fixing Common Oracle Database Problems
  • Internal Developer Portals: Modern DevOps's Missing Piece

Gradle Goodness: Handle Copying Duplicate Files

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Apr. 29, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

In Gradle we can configure how duplicate files should be handled by the Copy task. Actually we can configure how duplicate files are handled by any task that implements the CopySpec interface. For example archive tasks also implements this interface. We must use the setDuplicatesStrategymethod to configure how Gradle behaves. The parameter is a value of the enumeration DuplicatesStrategy. We can use the values from the enum class or use String values, which are automatically converted to enum DuplicatesStrategy values.

We can choose the following strategies:

  • include: default strategy where the last duplicate file 'wins'.
  • exclude: only the first found duplicate file is copied and 'wins'.
  • warn: shows a warning on the console, but the last duplicate file 'wins' like with the include strategy.
  • fail: the build fails where duplicate files are found.

The following build file create four task of type Copy, each with a different duplicate strategy. In the directories src/manual and src/website we have a file COPY.txt. The content is simply a text line respectively COPY from src/manual and COPY from src/website:

// For each duplicate strategy we create a copy task.
['warn', 'include', 'exclude', 'fail'].each { strategy ->
    task "copyDuplicatesStrategy${strategy.capitalize()}"(type: Copy) {
        from 'src/manual'
        from 'src/webroot'

        into "$buildDir/copy"

        // Only the value for this property differs for
        // each created task.
        duplicatesStrategy = strategy

        // Print the used duplicates strategy when 
        // the task starts.
        doFirst {
            println "Copying with duplicates strategy '${strategy}'."
        }

        // Print the contents of the copied file COPY.txt.
        doLast {
            println "Contents of COPY.txt:"
            println file("$buildDir/copy/COPY.txt").text
        }
    }
}

We can now invoke the four tasks and see how Gradle reacts:

$ gradle copyDuplicatesStrategyWarn
:copyDuplicatesStrategyWarn
Copying with duplicates strategy 'warn'.
Encountered duplicate path "COPY.txt" during copy operation configured with DuplicatesStrategy.WARN
Contents of COPY.txt:
COPY from src/webroot
BUILD SUCCESSFUL
Total time: 3.728 secs
$ gradle copyDuplicatesStrategyInclude
:copyDuplicatesStrategyInclude
Copying with duplicates strategy 'include'.
Contents of COPY.txt:
COPY from src/webroot
BUILD SUCCESSFUL
Total time: 2.744 secs
$ gradle copyDuplicatesStrategyExclude
:copyDuplicatesStrategyExclude
Copying with duplicates strategy 'exclude'.
Contents of COPY.txt:
COPY from src/manual
BUILD SUCCESSFUL
Total time: 2.784 secs
$ gradle copyDuplicatesStrategyFail
:copyDuplicatesStrategyFail
Copying with duplicates strategy 'fail'.
:copyDuplicatesStrategyFail FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':copyDuplicatesStrategyFail'.
> Encountered duplicate path "COPY.txt" during copy operation configured with DuplicatesStrategy.FAIL
* 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: 2.786 secs

Written with Gradle 2.3.


Gradle

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introduce a New API Quickly Using Spring Boot and Gradle
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Building AI Applications With Java and Gradle
  • Understanding Dependencies...Visually!

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!