1

I need to access the compiler target version from within a maven plugin.

I can access the property ${maven.compiler.target}, but that property may not be set explicitly.

I also thought about accessing the configuration of the maven-compiler-plugin, but the Configuration object of the Plugin is always null.

But maybe there is even a better approach.

What is the recommended way to access the compiler target version from within a maven plugin?

radlan
  • 2,393
  • 4
  • 33
  • 53
  • Are you looking for an approach that is inclusive of any IDE or irrespective of the IDE used? I'm thinking about places where the target would be set for a project... the target property you mention is one, in eclipse java facets you would find another, but it's a compile time switch, so without a place to set it I'm not sure how you'd know what it is. This question has some related discussion: https://stackoverflow.com/questions/7032965/how-do-i-figure-out-and-change-which-version-of-java-maven-is-using-to-execute – J E Carter II Aug 02 '18 at 13:55
  • 1
    Can you explain what kind of plugin you are writing ? Furthermore I would rely on conventions which means the `maven.compiler.target` and if it is not set fail the build. Accessing another plugin is a really bad idea...Be careful about `maven.compiler.release` for Java 9+... – khmarbaise Aug 02 '18 at 14:01
  • @JECarterII No, I search for a way to do this from a maven plugin. There should be no dependency on any IDE at all. – radlan Aug 03 '18 at 06:23
  • @khmarbaise The plugin is for conversion of i18n resource bundles to XLS and vice versa. Since Java 9 i18n resource bundles can the UTF-8 so I want do default to that encoding if the target is JDK9+ and ISO-8859-1 in all other cases (unless the user overrides this). So actually I do not want need to know the exact JDK, but only the decision JDK9+ or not. I do not like the idea of referring to `maven.compiler.target` since it is not _the_ convention. Configuriging it inside the plugin configuration of the `maven-compiler-plugin` is also convention. – radlan Aug 03 '18 at 06:25
  • Hm. interesting that you are saying `maven.compiler.target` is not a convention? You could reference `maven.compiler.release` which is intended and used for JDK9+ (but also for JDK 7 target etc.). This is used by maven-compiler-plugin...Apart from that I would recommend to migrate a project only once to JDK 9 (with UTF-8) and than stick with it cause everything below JDK9 will end in January(?) 2019 ...so the question is it worth the effort for such plugin? – khmarbaise Aug 03 '18 at 09:15
  • @khmarbaise Sorry, I didn't say that `maven.compiler.target` is no convention, but it is not the only convention. The same then goes for `maven.compiler.release` I think. And I think you misunderstood the purpose of the plugin. It's not a one-shot solution for migration of a project. It is a plugin that will be usable by many applications targeted for Java 8 and also for applications targeted at Java 9+. But it should behave reasonable in all cases (support for Java <9 may end in 2019, but the applications still exist and will be in use much further without migrating to a new JDK). – radlan Aug 03 '18 at 13:09

0 Answers0