3

Given a parent pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>acme-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <java-version>1.8</java-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <com.acme.dynamite-version>0.0.1-SNAPSHOT</com.acme.dynamite-version>
    // etc
    </properties>
</project>

and child pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>child</artifactId>
    <version>dev-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>com.acme</groupId>
        <artifactId>acme-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.acme</groupId>
            <artifactId>dynamite</artifactId>
            <version>${com.acme.dynamite-version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

The property, com.acme.dynamite-version, which is explicitly referenced in the child pom is resolved, however project.build.sourceEncoding is ignored. The Jenkins build of 'mvn clean install' throws a warning:

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

Is there a way to get the child pom to recognize project.build.sourceEncoding? This is not a multi module project, I am just trying to consolidate properties in one place

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118
  • As a Jenkins pre-build step: clean install. – Paul Croarkin Sep 19 '16 at 18:48
  • @Paul: what's the build order of the projects on Jenkins? – gtonic Sep 19 '16 at 19:29
  • The parent project is not in Jenkins (long story). I've done a mvn clean install locally on the parent and uploaded it directly to our Artifactory repo. The intention is to switch it to a release once all of these wrinkles are worked out. Thank you for taking the time to answer. – Paul Croarkin Sep 19 '16 at 20:43

2 Answers2

6

Properties defined in a parent POM are inherited in the child POM... but for that, the parent POM defining those properties needs to be installed before the child is built. And this is the issue here:

  1. Your parent, having a version 0.0.1-SNAPSHOT, right-fully declares the property <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>. However, that version of the parent defining this property is not installed in your local repository.
  2. When trying to build the child project, Maven will look-up any dependencies and parent POM in your local repo. It will find the version of the parent without that property and continue the build. Then, the maven-resources-plugin will emit this warning because no encoding have been set. (Setting the project.build.sourceEncoding also sets by default the encoding used by this plugin).

The solution is to build the parent first.

In a multi-module Maven project, you simply need to build the parent: Maven will order the reactor in such a way that dependant projects are built first; so in this case, the parent would be built first, and then the child (or module in this case), thereby ensuring that all properties defined in the parent are accessible to the child.

Outside of a multi-module Maven project, the idea is still the same, but you then need to perform two distinct builds: first the parent to install the right version into your local repository, and then the child. Note that when such parents are not used as aggregator projects, but more as building blocks consolidating common properties for children to inherit from, it would be preferable to have a distinct release cycle with it: consider making a release of your parent, so that every child can inherit that particular version.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
-2

You need to add the maven-resources-plugin in order to activate filtering, like here:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.0.1</version>
      <configuration>
        ...
        <encoding>UTF-8</encoding>
        ...
      </configuration>
    </plugin>
  </plugins>
  ...
</build>
gtonic
  • 2,295
  • 1
  • 24
  • 32
  • Thanks. Is this in the parent or the child pom? Why did someone vote this down? – Paul Croarkin Sep 19 '16 at 18:50
  • Add this to your parent pom. – gtonic Sep 19 '16 at 18:50
  • 3
    @Paul This answer is wrong. `project.build.sourceEncoding` is used by default, see the documentation https://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html#encoding – Tunaki Sep 19 '16 at 18:52
  • Obviously there are several approaches to this problem, like described here : http://stackoverflow.com/questions/3017695/how-to-configure-encoding-in-maven – gtonic Sep 19 '16 at 18:53
  • 1
    And this one isn't one of them. It is redefining a value to what it already is by default (because `UTF-8` was set). – Tunaki Sep 19 '16 at 18:53
  • Mea culpa. So what do we do here, flag as duplicate of http://stackoverflow.com/questions/3017695/how-to-configure-encoding-in-maven? – gtonic Sep 19 '16 at 18:57
  • Eh technically, it is not a duplicate of that question since the issue here is related to the build order of parent / child. OP already knows how to set the encoding. But there are still [pending questions](http://stackoverflow.com/questions/39580056/project-build-sourceencoding-defined-in-parent-pom-ignored-in-child#comment66470156_39580056) before deciding the dupe if there is one. – Tunaki Sep 19 '16 at 19:02
  • @tunaki - Are you saying that I should keep the encoding in the property and add the maven-resources-plugin without the encoding specified? Does the plugin go in the parent or the child? – Paul Croarkin Sep 19 '16 at 19:07
  • 1
    I'm saying that adding the plugin or not isn't relevant to your current problem. You can add it if you want, in order to set a version, but that won't solve your issue. – Tunaki Sep 19 '16 at 19:12
  • 1
    Well, please refer to my two comments [here](http://stackoverflow.com/questions/39580056/project-build-sourceencoding-defined-in-parent-pom-ignored-in-child#comment66470156_39580056)... The comments on this answer are starting to get too long. – Tunaki Sep 19 '16 at 19:14