<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nelson Osacky</title>
    <description>The latest articles on DEV Community by Nelson Osacky (@runningcode).</description>
    <link>https://dev.to/runningcode</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F428732%2Fef35cdbf-0e18-49a7-a987-15b259c95018.jpg</url>
      <title>DEV Community: Nelson Osacky</title>
      <link>https://dev.to/runningcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/runningcode"/>
    <language>en</language>
    <item>
      <title>A problem was found with the configuration of the Gradle 7 task</title>
      <dc:creator>Nelson Osacky</dc:creator>
      <pubDate>Wed, 02 Jun 2021 13:36:47 +0000</pubDate>
      <link>https://dev.to/runningcode/a-problem-was-found-with-the-configuration-of-the-gradle-7-task-3ej1</link>
      <guid>https://dev.to/runningcode/a-problem-was-found-with-the-configuration-of-the-gradle-7-task-3ej1</guid>
      <description>&lt;p&gt;Developers want builds to be fast, correct and reliable. In order to improve the reliability and correctness of Gradle builds, &lt;a href="https://docs.gradle.org/7.0/release-notes.html"&gt;Gradle 7&lt;/a&gt; has introduced stricter validation checks for tasks. This means that &lt;a href="https://docs.gradle.org/current/userguide/upgrading_version_6.html#task_validation_problems_are_now_errors"&gt;validation warnings in Gradle 6 have now turned in to errors in Gradle 7&lt;/a&gt;.&lt;br&gt;
When upgrading to Gradle 7, you may encounter an issue that some of your previously working plugins may not pass the validation and therefore fail your build. &lt;/p&gt;

&lt;p&gt;The error message in your build upon upgrading to Gradle 7 could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* What went wrong:
A problem was found with the configuration of task ':incompatibleTask' (type 'IncompatibleTask').
  - Type 'com.osacky.replace.example.IncompatibleTask' property 'foo' is missing an input or output annotation.

    Reason: A property without annotation isn't considered during up-to-date checking.

    Possible solutions:
      1. Add an input or output annotation.
      2. Mark it as @Internal.

    Please refer to https://docs.gradle.org/7.0.2/userguide/validation_problems.html#missing_annotation for more details about this problem.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Fixing first party plugins
&lt;/h1&gt;

&lt;p&gt;For tasks defined within your own build, the error message is actionable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To fix this problem, you need to annotate the property with the appropriate annotation, for example &lt;code&gt;@InputDirectory&lt;/code&gt; for a property representing an input directory, or &lt;code&gt;@OutputDirectory&lt;/code&gt; for a property representing an output directory.&lt;/p&gt;

&lt;p&gt;Alternatively, if the property is internal, that is to say that it shouldn’t participate in up-to-date checking (it’s not an input or an output), then you need to annotate it with &lt;code&gt;@Internal&lt;/code&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For tasks defined using the so-called ad-hoc api, &lt;a href="https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:runtime_api_for_adhoc"&gt;you can also add the input and output declarations&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Fixing third party plugins
&lt;/h1&gt;

&lt;p&gt;For third party plugins, &lt;a href="https://github.com/gradle/gradle/issues/16580"&gt;it isn't so clear what to do&lt;/a&gt;. First, check to see if there is an update to the Gradle plugin which makes the task compatible with Gradle 7.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get involved
&lt;/h2&gt;

&lt;p&gt;If no such updated version exists, this is a great time to get involved in an open source project. You can file an issue for that third party plugin to add the proper annotation to the task definition.&lt;br&gt;
Even better, you can also submit a pull request to add the proper annotations.&lt;/p&gt;

&lt;h1&gt;
  
  
  Unmaintained third party plugins
&lt;/h1&gt;

&lt;p&gt;For those unmaintained plugins, there is another option. You can redefine the task in your &lt;code&gt;buildSrc&lt;/code&gt; directory and declare the proper annotations. Here is how to do this.&lt;/p&gt;

&lt;p&gt;First, if you have not already done so, create a &lt;code&gt;buildSrc&lt;/code&gt; directory in your project and configure the &lt;code&gt;build.gradle&lt;/code&gt; file. &lt;a href="https://github.com/runningcode/replace-plugin-task/blob/master/buildSrc/build.gradle.kts"&gt;Here is a sample.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Second, copy paste the task class in to your &lt;code&gt;buildSrc&lt;/code&gt; project in your Gradle build. Make sure to paste it in to the proper package. Here &lt;a href="https://github.com/runningcode/replace-plugin-task/tree/master/buildSrc/src/main/java/com/osacky/replace/example"&gt;is a sample copy&lt;/a&gt; and &lt;a href="https://github.com/runningcode/replace-plugin-task/blob/master/plugin/src/main/java/com/osacky/replace/example/IncompatibleTask.kt"&gt;the task's original definition&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Third, &lt;a href="https://github.com/runningcode/replace-plugin-task/blob/master/buildSrc/src/main/java/com/osacky/replace/example/IncompatibleTask.kt#L9"&gt;add the missing annotation&lt;/a&gt; to the missing property defined in the task.&lt;/p&gt;

&lt;p&gt;Finally, run your Gradle build and enjoy!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/runningcode/replace-plugin-task"&gt;Here is a sample Gradle project&lt;/a&gt; demonstrating how to do this. The plugin is defined as a composite build in the &lt;a href="https://github.com/runningcode/replace-plugin-task/blob/master/plugin/build.gradle.kts#L18"&gt;&lt;code&gt;plugin&lt;/code&gt; project&lt;/a&gt;. It is included in the main build and &lt;a href="https://github.com/runningcode/replace-plugin-task/blob/master/lib/build.gradle#L3"&gt;applied in the &lt;code&gt;lib&lt;/code&gt; project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To run the sample, &lt;code&gt;./gradlew :lib:incompatibleTask&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To see the incompatible task fail the build in an integration test, run &lt;code&gt;./gradlew :plugin:test&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further resources
&lt;/h2&gt;

&lt;p&gt;If you are curious to understand more about how monkey patching classes with &lt;code&gt;buildSrc&lt;/code&gt; works and classloader hierarchy in Java, I recommend &lt;a href="https://dev.to/autonomousapps/abusing-gradle-s-class-loader-hierarchy-for-fun-and-profit-oca"&gt;reading this post&lt;/a&gt; by &lt;a class="mentioned-user" href="https://dev.to/autonomousapps"&gt;@autonomousapps&lt;/a&gt;
.&lt;/p&gt;

&lt;p&gt;Thanks to Tony (&lt;a class="mentioned-user" href="https://dev.to/autonomousapps"&gt;@autonomousapps&lt;/a&gt;
) for proofreading this blog post.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The opinions expressed in this post are the opinions of Nelson Osacky and not of his employer.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>gradle</category>
    </item>
  </channel>
</rss>
