How can a duplicate class be excluded from sbt assembly?












9















We have a situation in which two dependencies have exactly the same class (because one of the dependencies copied it and included in their own source).



This is causing sbt assembly to fail its deduplication checks.



How can I exclude a class from a particular jar?










share|improve this question





























    9















    We have a situation in which two dependencies have exactly the same class (because one of the dependencies copied it and included in their own source).



    This is causing sbt assembly to fail its deduplication checks.



    How can I exclude a class from a particular jar?










    share|improve this question



























      9












      9








      9


      5






      We have a situation in which two dependencies have exactly the same class (because one of the dependencies copied it and included in their own source).



      This is causing sbt assembly to fail its deduplication checks.



      How can I exclude a class from a particular jar?










      share|improve this question
















      We have a situation in which two dependencies have exactly the same class (because one of the dependencies copied it and included in their own source).



      This is causing sbt assembly to fail its deduplication checks.



      How can I exclude a class from a particular jar?







      sbt sbt-assembly






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 24 '14 at 9:57







      Channing Walton

















      asked Jun 23 '14 at 10:12









      Channing WaltonChanning Walton

      2,9032247




      2,9032247
























          2 Answers
          2






          active

          oldest

          votes


















          17














          You need a mergeStrategy, which will take one of the files.



          mergeStrategy in assembly := {
          case PathList("path", "to", "your", "DuplicatedClass.class") => MergeStrategy.first
          case x => (mergeStrategy in assembly).value(x)
          }


          Update



          If you want to handle the file depending on the JAR which it came from, I don't think you can with the merge strategies that assembly plugin defines. What you could do you could define your own strategy.



          I would invert your condition though. I think the question should be "How can I include a class from a particular JAR?". The reason is that there can be more than two JARs having the same class, and you can only include one in the end.



          You can tell from where the file comes by using AssemblyUtils.sourceOfFileForMerge.



          project/IncludeFromJar.scala



          import sbtassembly._
          import java.io.File
          import sbtassembly.Plugin.MergeStrategy

          class IncludeFromJar(val jarName: String) extends MergeStrategy {

          val name = "includeFromJar"

          def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
          val (tmp, path, files) = args
          val includedFiles = files.flatMap { f =>
          val (source, _, _, isFromJar) = sbtassembly.AssemblyUtils.sourceOfFileForMerge(tmp, f)
          if(isFromJar && source.getName == jarName) Some(f -> path) else None
          }
          Right(includedFiles)
          }

          }


          build.sbt



          mergeStrategy in assembly := {
          case PathList("path", "to", "your", "DuplicatedClass.class") => new IncludeFromJar("jarname.jar")
          case x => (mergeStrategy in assembly).value(x)
          }





          share|improve this answer





















          • 1





            It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

            – Jacek Laskowski
            Jun 23 '14 at 11:26






          • 1





            doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

            – Channing Walton
            Jun 24 '14 at 9:56








          • 1





            @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

            – lpiepiora
            Jun 24 '14 at 17:06






          • 1





            I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

            – javadba
            Jun 26 '14 at 16:05






          • 1





            in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

            – javadba
            Jun 26 '14 at 16:21





















          1














          What version of sbtassembly are yall using?
          I believe Im using using a different version (0.14.2) because Im getting an error using use project/IncludeFromJar.scala.



          When compiling I get the following error:



          method apply cannot override final member
          def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
          ^


          Upon further investigation I found out that sbtassembly.MergeStrategy's apply method is final. Therefore, IncludeFromJar's apply method cannot override sbtassembly.MergeStrategy's even with specifying override def apply in IncludeFromJar



          Thanks :)






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24363363%2fhow-can-a-duplicate-class-be-excluded-from-sbt-assembly%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            17














            You need a mergeStrategy, which will take one of the files.



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => MergeStrategy.first
            case x => (mergeStrategy in assembly).value(x)
            }


            Update



            If you want to handle the file depending on the JAR which it came from, I don't think you can with the merge strategies that assembly plugin defines. What you could do you could define your own strategy.



            I would invert your condition though. I think the question should be "How can I include a class from a particular JAR?". The reason is that there can be more than two JARs having the same class, and you can only include one in the end.



            You can tell from where the file comes by using AssemblyUtils.sourceOfFileForMerge.



            project/IncludeFromJar.scala



            import sbtassembly._
            import java.io.File
            import sbtassembly.Plugin.MergeStrategy

            class IncludeFromJar(val jarName: String) extends MergeStrategy {

            val name = "includeFromJar"

            def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
            val (tmp, path, files) = args
            val includedFiles = files.flatMap { f =>
            val (source, _, _, isFromJar) = sbtassembly.AssemblyUtils.sourceOfFileForMerge(tmp, f)
            if(isFromJar && source.getName == jarName) Some(f -> path) else None
            }
            Right(includedFiles)
            }

            }


            build.sbt



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => new IncludeFromJar("jarname.jar")
            case x => (mergeStrategy in assembly).value(x)
            }





            share|improve this answer





















            • 1





              It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

              – Jacek Laskowski
              Jun 23 '14 at 11:26






            • 1





              doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

              – Channing Walton
              Jun 24 '14 at 9:56








            • 1





              @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

              – lpiepiora
              Jun 24 '14 at 17:06






            • 1





              I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

              – javadba
              Jun 26 '14 at 16:05






            • 1





              in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

              – javadba
              Jun 26 '14 at 16:21


















            17














            You need a mergeStrategy, which will take one of the files.



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => MergeStrategy.first
            case x => (mergeStrategy in assembly).value(x)
            }


            Update



            If you want to handle the file depending on the JAR which it came from, I don't think you can with the merge strategies that assembly plugin defines. What you could do you could define your own strategy.



            I would invert your condition though. I think the question should be "How can I include a class from a particular JAR?". The reason is that there can be more than two JARs having the same class, and you can only include one in the end.



            You can tell from where the file comes by using AssemblyUtils.sourceOfFileForMerge.



            project/IncludeFromJar.scala



            import sbtassembly._
            import java.io.File
            import sbtassembly.Plugin.MergeStrategy

            class IncludeFromJar(val jarName: String) extends MergeStrategy {

            val name = "includeFromJar"

            def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
            val (tmp, path, files) = args
            val includedFiles = files.flatMap { f =>
            val (source, _, _, isFromJar) = sbtassembly.AssemblyUtils.sourceOfFileForMerge(tmp, f)
            if(isFromJar && source.getName == jarName) Some(f -> path) else None
            }
            Right(includedFiles)
            }

            }


            build.sbt



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => new IncludeFromJar("jarname.jar")
            case x => (mergeStrategy in assembly).value(x)
            }





            share|improve this answer





















            • 1





              It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

              – Jacek Laskowski
              Jun 23 '14 at 11:26






            • 1





              doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

              – Channing Walton
              Jun 24 '14 at 9:56








            • 1





              @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

              – lpiepiora
              Jun 24 '14 at 17:06






            • 1





              I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

              – javadba
              Jun 26 '14 at 16:05






            • 1





              in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

              – javadba
              Jun 26 '14 at 16:21
















            17












            17








            17







            You need a mergeStrategy, which will take one of the files.



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => MergeStrategy.first
            case x => (mergeStrategy in assembly).value(x)
            }


            Update



            If you want to handle the file depending on the JAR which it came from, I don't think you can with the merge strategies that assembly plugin defines. What you could do you could define your own strategy.



            I would invert your condition though. I think the question should be "How can I include a class from a particular JAR?". The reason is that there can be more than two JARs having the same class, and you can only include one in the end.



            You can tell from where the file comes by using AssemblyUtils.sourceOfFileForMerge.



            project/IncludeFromJar.scala



            import sbtassembly._
            import java.io.File
            import sbtassembly.Plugin.MergeStrategy

            class IncludeFromJar(val jarName: String) extends MergeStrategy {

            val name = "includeFromJar"

            def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
            val (tmp, path, files) = args
            val includedFiles = files.flatMap { f =>
            val (source, _, _, isFromJar) = sbtassembly.AssemblyUtils.sourceOfFileForMerge(tmp, f)
            if(isFromJar && source.getName == jarName) Some(f -> path) else None
            }
            Right(includedFiles)
            }

            }


            build.sbt



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => new IncludeFromJar("jarname.jar")
            case x => (mergeStrategy in assembly).value(x)
            }





            share|improve this answer















            You need a mergeStrategy, which will take one of the files.



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => MergeStrategy.first
            case x => (mergeStrategy in assembly).value(x)
            }


            Update



            If you want to handle the file depending on the JAR which it came from, I don't think you can with the merge strategies that assembly plugin defines. What you could do you could define your own strategy.



            I would invert your condition though. I think the question should be "How can I include a class from a particular JAR?". The reason is that there can be more than two JARs having the same class, and you can only include one in the end.



            You can tell from where the file comes by using AssemblyUtils.sourceOfFileForMerge.



            project/IncludeFromJar.scala



            import sbtassembly._
            import java.io.File
            import sbtassembly.Plugin.MergeStrategy

            class IncludeFromJar(val jarName: String) extends MergeStrategy {

            val name = "includeFromJar"

            def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
            val (tmp, path, files) = args
            val includedFiles = files.flatMap { f =>
            val (source, _, _, isFromJar) = sbtassembly.AssemblyUtils.sourceOfFileForMerge(tmp, f)
            if(isFromJar && source.getName == jarName) Some(f -> path) else None
            }
            Right(includedFiles)
            }

            }


            build.sbt



            mergeStrategy in assembly := {
            case PathList("path", "to", "your", "DuplicatedClass.class") => new IncludeFromJar("jarname.jar")
            case x => (mergeStrategy in assembly).value(x)
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 19 at 21:12









            Michael Mior

            21.5k66292




            21.5k66292










            answered Jun 23 '14 at 10:35









            lpiepioralpiepiora

            12.1k12741




            12.1k12741








            • 1





              It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

              – Jacek Laskowski
              Jun 23 '14 at 11:26






            • 1





              doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

              – Channing Walton
              Jun 24 '14 at 9:56








            • 1





              @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

              – lpiepiora
              Jun 24 '14 at 17:06






            • 1





              I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

              – javadba
              Jun 26 '14 at 16:05






            • 1





              in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

              – javadba
              Jun 26 '14 at 16:21
















            • 1





              It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

              – Jacek Laskowski
              Jun 23 '14 at 11:26






            • 1





              doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

              – Channing Walton
              Jun 24 '14 at 9:56








            • 1





              @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

              – lpiepiora
              Jun 24 '14 at 17:06






            • 1





              I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

              – javadba
              Jun 26 '14 at 16:05






            • 1





              in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

              – javadba
              Jun 26 '14 at 16:21










            1




            1





            It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

            – Jacek Laskowski
            Jun 23 '14 at 11:26





            It was indeed, but it's better so much now. Thanks for the change! People get used to the new operators sooner.

            – Jacek Laskowski
            Jun 23 '14 at 11:26




            1




            1





            doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

            – Channing Walton
            Jun 24 '14 at 9:56







            doesn't this solution take the first class it comes across ignoring where it came from? I want to exclude a class that should not be in a jar I depend on so I need to specify that class in that jar.

            – Channing Walton
            Jun 24 '14 at 9:56






            1




            1





            @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

            – lpiepiora
            Jun 24 '14 at 17:06





            @ChanningWalton I think you have to write your custom MergeStrategy to do that. Please check my updated answer.

            – lpiepiora
            Jun 24 '14 at 17:06




            1




            1





            I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

            – javadba
            Jun 26 '14 at 16:05





            I am getting "projectIncludeFromJar.scala:1: not found: object sbtassembly"

            – javadba
            Jun 26 '14 at 16:05




            1




            1





            in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

            – javadba
            Jun 26 '14 at 16:21







            in project/plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2"). AFA imports: I copied your code verbatim into project/IncludeFromJar.scala and intellij shows no errors but it fails from sbt command line.

            – javadba
            Jun 26 '14 at 16:21















            1














            What version of sbtassembly are yall using?
            I believe Im using using a different version (0.14.2) because Im getting an error using use project/IncludeFromJar.scala.



            When compiling I get the following error:



            method apply cannot override final member
            def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
            ^


            Upon further investigation I found out that sbtassembly.MergeStrategy's apply method is final. Therefore, IncludeFromJar's apply method cannot override sbtassembly.MergeStrategy's even with specifying override def apply in IncludeFromJar



            Thanks :)






            share|improve this answer




























              1














              What version of sbtassembly are yall using?
              I believe Im using using a different version (0.14.2) because Im getting an error using use project/IncludeFromJar.scala.



              When compiling I get the following error:



              method apply cannot override final member
              def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
              ^


              Upon further investigation I found out that sbtassembly.MergeStrategy's apply method is final. Therefore, IncludeFromJar's apply method cannot override sbtassembly.MergeStrategy's even with specifying override def apply in IncludeFromJar



              Thanks :)






              share|improve this answer


























                1












                1








                1







                What version of sbtassembly are yall using?
                I believe Im using using a different version (0.14.2) because Im getting an error using use project/IncludeFromJar.scala.



                When compiling I get the following error:



                method apply cannot override final member
                def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
                ^


                Upon further investigation I found out that sbtassembly.MergeStrategy's apply method is final. Therefore, IncludeFromJar's apply method cannot override sbtassembly.MergeStrategy's even with specifying override def apply in IncludeFromJar



                Thanks :)






                share|improve this answer













                What version of sbtassembly are yall using?
                I believe Im using using a different version (0.14.2) because Im getting an error using use project/IncludeFromJar.scala.



                When compiling I get the following error:



                method apply cannot override final member
                def apply(args: (File, String, Seq[File])): Either[String, Seq[(File, String)]] = {
                ^


                Upon further investigation I found out that sbtassembly.MergeStrategy's apply method is final. Therefore, IncludeFromJar's apply method cannot override sbtassembly.MergeStrategy's even with specifying override def apply in IncludeFromJar



                Thanks :)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 20 '18 at 14:04









                Joshua E. JodestyJoshua E. Jodesty

                112




                112






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24363363%2fhow-can-a-duplicate-class-be-excluded-from-sbt-assembly%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre