Skip to content

jakarta.inject support #2058

Open
Open

Description

@cogman

Jakarta is coming to cause headaches for all :)

It would be nice if Dagger could support processing Jakarta inject annotations as well as javax inject annotations. That would prevent us from potentially having to write a cross injection system constructor like

@Inject
@jakarta.inject.Inject

Fortunately, they don't share a package and it looks like jakarta pretty much only changed the package name.

Activity

  1. bcorso commented on Aug 20, 2020

    @bcorso

    I don't know what Jakarta is , but do you know why it is defining its own jsr330 annotations rather than using the standard javax.inject.* annotations?

    Have you tried filing a bug with them to switch?

  2. tbroyer commented on Aug 20, 2020

    @tbroyer

    @bcorso This is the new home for Java EE! Oracle killed javax.* and the Java EE name and forced Eclipse Jakarta to rename to jakarta.* and Jakarta EE (also applies to javax.servlet et al.)
    This is a copyright and Oracle-bullshit issue, there's no way backwards 🤷‍♂️

  3. bcorso commented on Aug 20, 2020

    @bcorso

    Oh... wow. Okay, yeah looks like we'll have to support all of jakarta.inject then.

  4. twillouer commented on Dec 10, 2020

    @twillouer

    Hi, with Jersey 3.0 it will be mandatory I think :)

  5. overheadhunter commented on Jun 30, 2021

    @overheadhunter

    Just a thought:

    Instead of supporting both, javax.inject and jakarta.inject, would switching to solely supporting jakarta.inject be an option? For downstream projects this means s/import javax.inject/import jakarta.inject/g. Nothing particularly difficult, but still technically an API-breaking change.

    If this was acceptable, implementing this change wouldn't increase the complexity in Dagger.

  6. Chang-Eric commented on Jun 30, 2021

    @Chang-Eric
    Member

    Thanks for the idea, but for a large breaking change like that we'd have to support both at the same time for some (long) period of time to let people incrementally migrate.

  7. overheadhunter commented on Jul 8, 2021

    @overheadhunter

    I just learned from the discussions in this PR, that jakarta.inject before version 2.0.0 still uses the old package name. So in fact this would be no breaking change when simply changing the dependency to jakarta.inject:jakarta.inject-api:1.0.3. These are these consequences:

    downstream project depends only on Dagger depends on Dagger and javax.inject
    uses module path ✅ Can now use reliable Automatic-Module-Name ⚠️ Split package
    uses classpath ✅ No change ✅ Classes from both jars are identical by design, classloader may choose arbitrarily

    The split package is only a virtual problem, since projects relying on the module path will avoid any such configuration. Nevertheless it should be documented.

    It is even possible to have jakarta.inject:jakarta.inject-api:1.0.3 and jakarta.inject:jakarta.inject-api:2.0.0 on the module path at the same time, since the former uses java.inject (sic! as per JEP 200) and the latter jakarta.inject automatic module names.

  8. overheadhunter commented on Jul 18, 2021

    @overheadhunter

    Just to confirm my previous suggestion: I just replaced javax.inject with jakarta.inject-api version 1.0.3 via dependency management in a multi-module project structure. No changes required. Works flawlessly on both modular and legacy projects.

    Can you please consider this as a preliminary solution, until a "real" switch to jakarta namespace can happen eventually?

  9. Chang-Eric commented on Jul 19, 2021

    @Chang-Eric
    Member

    Just to confirm your goals with this, this is so that you can publish a library with a named module that doesn't rely on the filename for the javax dependency since the jakarta dep provides an automatic module name?

    If we switched Dagger's dependency to jakarta 1.0.3 for the javax annotations, I'm concerned this might pose a problem when people start migrating since if you need jakarta 2.0.0 for the jakarta annotations, I think Gradle's dependency resolution will just choose the 2.0.0 version and then the old usages of javax will break. I know you said above it is possible to have both 1.0.3 and 2.0.0 on the classpath and module path, but I feel like them being different versions of the same artifact is going to make that complicated for some build systems.

  10. overheadhunter commented on Jul 19, 2021

    @overheadhunter

    Just to confirm your goals with this, this is so that you can publish a library with a named module that doesn't rely on the filename for the javax dependency since the jakarta dep provides an automatic module name?

    Correct. In fact it has to be exactly 1.0.3. Older versions use a wrong module name, newer versions require you to change your imports.

    If we switched Dagger's dependency to jakarta 1.0.3 for the javax annotations, I'm concerned this might pose a problem when people start migrating since if you need jakarta 2.0.0 for the jakarta annotations, I think Gradle's dependency resolution will just choose the 2.0.0 version and then the old usages of javax will break. I know you said above it is possible to have both 1.0.3 and 2.0.0 on the classpath and module path, but I feel like them being different versions of the same artifact is going to make that complicated for some build systems.

    You're right, I don't know any build tool that allows having two versions of the same dependency simultaneously... 😔

  11. tbroyer commented on Jul 20, 2021

    @tbroyer

    Having mixes of javax. and jakarta. artifacts is already a reality, and for some people mixes of javax. andjakarta. packages too (to a lesser extent), so having to manage your dependencies to select the appropriate dependency is already something we have to do (I suspect many people live with the mix of artifacts and duplicated classes).

    So, we're getting off topic here as the original issue was about the packages, and this should probably be discussed in #2636 instead; but choosing jakarta.inject artifacts would:

    • be a "breaking" change anyway (could cause duplicate classes in downstream projects, from javax.inject ad jakarta.inject artifacts)
    • allow publishing artifacts that depend on Dagger and are JPMS modules, without having to manage dependencies to switch javax.inject to jakarta.inject in that project.

    My opinion: I think I'd prefer that people manage their dependencies to replace javax.inject with jakarta.inject when they want to publish JPMS modules that depend on Dagger. This would put the burden on those projects and projects that use them, rather than every single project that uses Dagger.

    <dependencies>
      <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>2.x</version>
        <exclusions>
          <exclusion>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>jakarta.inject</groupId>
        <artifactId>jakarta.inject-api</artifactId>
        <version>1.0.3</version>
      </dependency>
    </dependencies>
  12. overheadhunter commented on Jul 20, 2021

    @overheadhunter

    I think I'd prefer that people manage their dependencies to replace javax.inject with jakarta.inject when they want to publish JPMS modules that depend on Dagger.

    While dependency management works great for applications (as I have previously suggested), it really isn't ideal for libraries. Since you explicitly mention publishing modules, I assume you have libraries in mind here.

    If a library starts to manage its dependencies, the library's dependents will "unmanage" this by simply depending on dagger/javax.inject/jakarta.inject themselves (as long as the configuration is not replicated everywhere downstream), causing even more confusion.

    Either way some projects will run into problems. So we're back to the original proposal:

    • either a breaking change in a major release
    • or as proposed by OP "support processing Jakarta inject annotations as well as javax inject annotations"

    Obviously, the latter is the most flexible, allowing downstream projects to decide what annotations library to depend on.

  13. 25 remaining items

  14. r0main commented on Apr 4, 2023

    @r0main

    Hi, same question as @ThomasVitale :-) Is there any news on the subject ?

    I have code that is shared between a Spring Boot app and an AWS Lambda. The shared code depends on javax.*. My Spring Boot app uses Spring as DI (obviously) and my AWS Lambda uses Dagger as DI (as Dagger is faster to startup).
    I would like to upgrade Spring Boot to version 3. So I nee to migrate the shared code to Jakarta EE 9 (it mandatory when using Spring Boot 3). And you see me coming... If I upgrade the shared code to Jakarta EE 9, I need a Dagger version compatible with it to keep my AWS Lambda working :-)

    Thank you in advance for the news :-)

    Best regards !

  15. Chang-Eric commented on Apr 26, 2023

    @Chang-Eric
    Member

    I'm going to try to add Provider support soon, which should cover all of the symbols for Jakarta. Cutting the dep to javax to fix the JPMS issues will likely take more time though.

  16. m-presnov commented on Nov 24, 2023

    @m-presnov

    Any news about support jakarta.inject.Provider in dagger-compiler?

  17. Chang-Eric commented on Nov 27, 2023

    @Chang-Eric
    Member

    I actually have a change ready for the first step of adding our own internal Dagger Provider type. I'm hoping to get it out soon. We ended up not being able to just fix it with dagger.internal.Factory because unsurprisingly a lot of our code references javax.inject.Provider directly and that all needs to be replaced. It also breaks the signature of a lot of our runtime methods, so in order to maintain backwards compatibility, there's extra code to detect and handle different backwards compatibility situations between libraries when we generate code.

    Frankly, it's been a huge pain even though it seems like a simple rename, which is why this is taking so long. Also, since this change has the potential to break a lot of things, we're trying to isolate it into its own release which is causing some delays too. Hopefully this first step should be out soon. It won't let you break the javax dependency yet, but once that is out and there aren't hopefully any issues, we should hopefully be able to then release an artifact that has our dagger.internal.Provider only extend the Jakarta version of Provider. Note that even with all that, there's still going to be complications with our SPI and how we're going to break some external plugins if we change the binding keys that have Provider in them to the Jakarta type (e.g. some of our Map<K, Provider<V>> bindings), so there's still something to figure out there too.

  18. jeffalder commented on Dec 5, 2023

    @jeffalder

    @Chang-Eric How can we help? Do you have things that need coding? Testing? I'm very interested in helping the team figure this out. Clarifying "this converts to Jakarta, this does not" has been a pain point for folks I support that are using Jakarta and Dagger 2, so I'm motivated.

  19. Chang-Eric commented on Dec 7, 2023

    @Chang-Eric
    Member

    I just submitted a change in 75d3cbc to add the new dagger.internal.Provider. This won't change anything with Jakarta deps yet, but it is the next step. Hopefully we'll release this soon, so people can start building with this, and then I'll do a change to allow using the Jakarta version in user code. We'll also have to do a change that will break compatibility across versions because unfortunately some of our factories are generated returning the javax.inject.Provider directly, but hopefully that will only be necessary once we actually try to drop the javax dependency.

    One way you can help is if you want to try out the current change at head to make sure there isn't any issue. https://dagger.dev/dev-guide/versions has info on how to depend on the HEAD-SNAPSHOT.

  20. jeffalder commented on Dec 13, 2023

    @jeffalder

    I ran a quick test on one of my repos with HEAD-SNAPSHOT and it was fine (as I expected). We don't generally do anything too fancy with Dagger, we don't integrate directly with the Provider classes, and we rarely (if ever?) even share Dagger generated code. I kind of expect this effort to be mostly trivial for us.

  21. r0main commented on Dec 15, 2023

    @r0main

    Hi,

    We have a small project using Dagger. We also tested the head-snapshot version and it worked fine ! Thank you for the work !

    I have a question : I had to replace javax package by jakarta package in my code. That was absolutely expected. But in the generated code by Dagger, in *_Factory classes, I saw that there is style javax import, is it normal ? Is it releated to your last message @Chang-Eric ? I admit I didn't understand everything but you're talking about factories and javax :-)

    Hope this test can help even if it's small !

  22. dnouls commented on Mar 26, 2024

    @dnouls

    Any idea if support for Jakarta API's is going to be supported with Dagger? We need to migrate to newer versions of software for security and maintainability reasons, so the problem with Jakarta is starting to become quite a big issue.

  23. jeffalder commented on Mar 26, 2024

    @jeffalder

    Any idea if support for Jakarta API's is going to be supported with Dagger? We need to migrate to newer versions of software for security and maintainability reasons, so the problem with Jakarta is starting to become quite a big issue.

    @dnouls My experience is that I was able to update my server (e.g., Netty) to Jakarta, but retain the javax.inject API for use with Dagger. In our case, the two were not coupled together. Are you using anything that would force them to use the same packages?

  24. Chang-Eric commented on Mar 27, 2024

    @Chang-Eric
    Member

    Any idea if support for Jakarta API's is going to be supported with Dagger?

    Jakarta APIs like @jakarta.inject.Inject should currently work except for using jakarta.inject.Provider. I'm currently actually working on that (usually a bit each week as I have time), but am running into lots of issues internally that keep blocking this. Unfortunately I don't have an ETA, but this actually is being worked on. It is just slow going as new issues pop up and I don't always have a lot time to dedicate to it.

  25. Junzz0 commented on Jul 3, 2025

    @Junzz0

    @Chang-Eric Dagger(Desktop) seems to be able to switch to jakarta.inject, but Hilt still relies on javax.inject

      import javax.inject.Singleton;
    
      @Component(
          modules = {
              ApplicationContextModule.class,
              CoroutineScopesModule.class,
              DaosModule.class,
              DataModule.class,
              DataStoreModule.class,
              DatabaseModule.class,
              DispatchersModule.class,
              HiltWrapper_FragmentGetContextFix_FragmentGetContextFixModule.class,
              HiltWrapper_WorkerFactoryModule.class,
              NetworkModule.class,
              ActivityRetainedCBuilderModule.class,
              ServiceCBuilderModule.class,
              SyncWorker_HiltModule.class
          }
      )
      @Singleton
      public abstract static class SingletonC implements Application_GeneratedInjector,
          HiltWrapper_HiltWorkerFactoryEntryPoint,
          FragmentGetContextFix.FragmentGetContextFixEntryPoint,
          HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint,
          ServiceComponentManager.ServiceComponentBuilderEntryPoint,
          SingletonComponent,
          GeneratedComponent {
      }
    

    But it's not clear to me if it's a problem with the dagger hilt or androidx hilt.

    Similar to #4780


    #3916 (comment)

    Abandon the migration to jakarta.inject

    Giving an update as some things have changed since my last update a year ago and we never really gave an update on our new plans.

    Over the past months we have been working to support KMP and so this is now actually on our roadmap. We have been migrating to XPoet to support Kotlin generation, but even with that nearing completion, there are still a number of hurdles to cross like issues with nullability, visibility, generic types, and dependencies like our dependency on javax and jakarta. There’s no clear date when this will be done, especially since we’re still working on supporting KSP2 which is in itself a blocker for KMP, but we thought we should update this since we are at least working on it.

    It basically means the end of Hilt/Dagger

    We’re still here =)

    I believe that after Dagger supports KMP, there will naturally be a good choice 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions