Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties in pom are not resolved for maven-install-plugin #256

Closed
phauer opened this issue Jul 2, 2016 · 23 comments
Closed

Properties in pom are not resolved for maven-install-plugin #256

phauer opened this issue Jul 2, 2016 · 23 comments

Comments

@phauer
Copy link

phauer commented Jul 2, 2016

Hi,
I try to use the Git commit hash (and the commit timestamp) as the version number for my built artifact.
mvn package works fine. It creates a myapp-20160702-180249.193a613.jar.

However, "mvn install" doesn't seem to resolve the dynamic properties of the git-commit-id-plugin (like ${git.commit.time} and ${git.commit.id.abbrev}). Output:

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ versioning-continuous-delivery --- [INFO] Installing <projectdir>/target/app-20160702-180249.193a613.jar to <userhome>/.m2/repository/<path>/app/${git.commit.time}.${git.commit.id.abbrev}/app-${git.commit.time}.${git.commit.id.abbrev}.jar
Same problem for "mvn deploy".

My pom.xml:

<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>${version.number}</version>
...
<properties>
    <version.number>${git.commit.time}.${git.commit.id.abbrev}</version.number>
</properties>
...
<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dateFormat>yyyyMMdd-HHmmss</dateFormat>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <generateGitPropertiesFile>false</generateGitPropertiesFile>
    </configuration>
</plugin>

Basically, it seems that properties are resolved (like ${version.number}), but not the one created by the git-commit-id-plugin (${git.commit.id.abbrev}). That's why I'm posting this issue here.

Cheers Philipp

@ktoso
Copy link
Collaborator

ktoso commented Jul 2, 2016

This is very likely to be resolved by what is explained in the readme:

Note : In order to be able to validate the generated git-properties inside the pom
itself you may need to set the configuration <injectAllReactorProjects>true</injectAllReactorProjects>.

@phauer
Copy link
Author

phauer commented Jul 2, 2016

Thanks for your response. Unfortunately, this doesn't help. But my project is not a multi-module project anyway.

@ktoso
Copy link
Collaborator

ktoso commented Jul 2, 2016

Have you played around with phases?
https://github.com/ktoso/maven-git-commit-id-plugin#using-the-plugin (a bit explained there in comments)

@phauer
Copy link
Author

phauer commented Jul 3, 2016

I'm not sure, what exactly do you want me to try out. Changing the phase of the revision-goal?

By the way, I'm using version 2.2.1.

@TheSnoozer
Copy link
Collaborator

Ad to the configuration of the git plugin the following <injectAllReactorProjects>true</injectAllReactorProjects>
Result:

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dateFormat>yyyyMMdd-HHmmss</dateFormat>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <generateGitPropertiesFile>false</generateGitPropertiesFile>
        <injectAllReactorProjects>true</injectAllReactorProjects>
    </configuration>
</plugin>

Also what happens if you set the finalName:

<build>
        <finalName>${git.commit.time}.${git.commit.id.abbrev}</finalName>
</build>

?

@phauer
Copy link
Author

phauer commented Jul 3, 2016

Thanks for your response. Unfortunately, setting injectAllReactorProjects and the finalName doesn't help.

@aszka
Copy link

aszka commented Jul 14, 2016

I used previous version of plugin, but maybe you should try add part with <resources></resources>. It helps me.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <finalName>${project.name}</finalName>
    <plugins>
        <plugin>
                something
        </plugin>
        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>2.1.13</version>
            <executions>
                <execution>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

@phauer
Copy link
Author

phauer commented Jul 15, 2016

Hi aszka,
adding <resources> doesn't help either. Can nobody reproduce my problem?
I created a little project to demonstrate the issue. Checkout this project:
https://github.com/phauer/blog-related/tree/createJar/versioning-continuous-delivery
and call mvn install.

@TheSnoozer
Copy link
Collaborator

The build log seems to indicates the issue here:

$ mvn clean install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for de.philipphauer.blog:versioning-continuous-delivery:jar:${git.commit.time}.${git.commit.id.abbrev}
[WARNING] 'version' contains an expression but should be a constant. @ de.philipphauer.blog:versioning-continuous-delivery:${version.number}, /tmp/blog-related/versioning-continuous-delivery/pom.xml, line 8, column 11
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 

Change the <version>${version.number}</version> to a constant like <version>1.0-SNAPSHOT</version>, add the config <injectAllReactorProjects>true</injectAllReactorProjects> to the git plugin and define the final name of the jar / war with the finalName-Tag within the build-Tag.

<project ....>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.number>${git.commit.time}.${git.commit.id.abbrev}</version.number>
    </properties>
    <build>
        <finalName>${version.number}</finalName>
        <plugins>
            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dateFormat>yyyyMMdd-HHmmss</dateFormat><!--  human-readable part of the version id -->
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                    <generateGitPropertiesFile>false</generateGitPropertiesFile><!-- somehow necessary. otherwise the variables are not available in the pom -->
                    <injectAllReactorProjects>true</injectAllReactorProjects>
                </configuration>
            </plugin>
......

Can't build this specific project since I don't have docker, but have tested with a similar project where I could reproduce the problem.

Hope it helps.

@phauer
Copy link
Author

phauer commented Aug 5, 2016

Hi TheSnoozer, thanks for your help and feedback!
Unfortunately, this doesn't work for me. I changed my playground branch according to your advise:
https://github.com/phauer/blog-related/tree/createJar/versioning-continuous-delivery (you don't need docker to call mvn install on this branch).

The point is that I can specify the jar name with <finalName>, but when it comes to installing the jar to my local maven repository (mvn install), the value from <version> is used. When I set <version> to 1.0.0-SNAPSHOT, the artifact is placed under .../1.0.0-SNAPSHOT/myapp-1.0.0-SNAPSHOT.jar and not under .../20160715-084735.87788e4/myapp-20160715-084735.87788e4.jar. So this is the wrong version number.

The output is something like this:

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ versioning-continuous-delivery ---
[INFO] Installing /home/<path>/target/myapp-20160715-084735.87788e4.jar to /home/<user>/.m2/repository/<groupId>/myapp/1.0.0-SNAPSHOT/myapp-1.0.0-SNAPSHOT.jar

@TheSnoozer
Copy link
Collaborator

Hi,
I actually tried to get it working but it seems that the maven-install-plugin does not want to take the properties into account.
After some digging I found this which basically is the same issue as you have or would like to get solved:
https://issues.apache.org/jira/browse/MINSTALL-1

In short: maven-install-plugin will not take finalName into account!
Also applying the version number via command line is not supported (https://issues.apache.org/jira/browse/MINSTALL-30).

On top using a dynamic version number like <version>${git.commit.time}.${git.commit.id.abbrev}</version> will result in warning that a non constant is used, hence all options I currently see are not supported or result in a warning.

I would quote from https://issues.apache.org/jira/browse/MINSTALL-30

For continuous integration you shouldn't abuse the pom.xml, the file which contains the build-**instructions**. If you want to uniquely identify the build-**result** you can add a revision-number to the MANIFEST-file (see buildnumber-maven-plugin) or add your custom file to the artifact.

At this stage I feel that this is not a problem of the git-commit-id-plugin it's a general maven / maven-install-plugin issue....
Nevertheless I would point out that your problem can be solved by using the maven-install-plugin with the goal install-file.

Use recommended steps with finalName from above and add the the following plugins:

            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default-install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default-install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>${project.basedir}/target/${artifactId}-${version.number}.${project.packaging}</file>
                    <groupId>de.philipphauer.blog</groupId>
                    <artifactId>versioning-continuous-delivery</artifactId>
                    <version>${artifactId}-${version.number}</version>
                    <packaging>jar</packaging>
                </configuration>
            </plugin>

You would need to overwrite and skip the default install-Plugin otherwise you would install a version as specified in the <version>-Tag. Command-Line output is the following:

[INFO] --- maven-install-plugin:2.4:install-file (default-install) @ versioning-continuous-delivery ---
[INFO] Installing /tmp/blog-related/versioning-continuous-delivery/target/versioning-continuous-delivery-20160805-230836.9be8cc7.jar to ~/.m2/repository/de/philipphauer/blog/versioning-continuous-delivery/versioning-continuous-delivery-20160805-230836.9be8cc7/versioning-continuous-delivery-versioning-continuous-delivery-20160805-230836.9be8cc7.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ versioning-continuous-delivery ---
[INFO] Skipping artifact installation
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Full pom that I used can be obtained from here:
https://gist.github.com/TheSnoozer/ddce23f6ee86443ccc397389e94351f5

@theiamdude
Copy link

I am having the same issue exactly.
How about the deploy phase? would there be any specific configuration required for the maven-deploy-plugin?

@TheSnoozer
Copy link
Collaborator

Please review my last comment in this issue here:
#256 (comment)

This specific behaviour is basically not intended / not supported by maven-install-plugin and maven-deploy-plugin (https://issues.apache.org/jira/browse/MDEPLOY-93). The naming format in the remote repo is always $artifactId-$version-$classifier by default.

However as outlined in my previous comment you can override the configuration from plugins and use install-file mojo for maven-install-plugin or deploy-file mojo for maven-deploy-plugin (example):

maven-install-plugin

            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default-install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default-install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>${project.basedir}/target/${artifactId}-${version.number}.${project.packaging}</file>
                    <groupId>de.philipphauer.blog</groupId>
                    <artifactId>versioning-continuous-delivery</artifactId>
                    <version>${artifactId}-${version.number}</version>
                    <packaging>jar</packaging>
                </configuration>
            </plugin>

read more here: https://maven.apache.org/plugins/maven-install-plugin/

deploy-install-plugin

            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>${project.basedir}/target/${version.number}.${project.packaging}</file>
                    <groupId>de.philipphauer.blog</groupId>
                    <artifactId>versioning-continuous-delivery</artifactId>
                    <version>${artifactId}-${version.number}</version>
                    <packaging>jar</packaging>
                    <url>ftp://user:12345@localhost/</url>
                </configuration>
            </plugin>

Please note that the url from the maven-deploy-plugin contains a username / password for testing purposes.

read more here: https://maven.apache.org/plugins/maven-deploy-plugin/

Disclaimer

I would outline that this is just a sample configuration on how it can be done and would strongly advice not to use a pattern that is not build-in within maven.

@theiamdude
Copy link

Thanks for you update @TheSnoozer. That provided a possible solution moving forward.

@YoshiMan
Copy link

YoshiMan commented Dec 26, 2017

Hey guys,
hey @phauer,

I think I found another solution, could you verify, if this is practicable to?
After a long debug session with the maven-install-plugin, I found this solution.
In my theory the maven-install-plugin uses the project to operate.
see

// from org.apache.maven.plugin.install.InstallMojo and org.apache.maven.plugins.deploy.DeployMojo:
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;

And the project.getArtifact()-call will give always the same reference. I think the project-object is build during the maven startup and never changed. So we have to change the artifact reference in order to get the maven-install-plugin and maven-deploy-plugin to work the way we want. ;)

This will work with pom packaging and is less configuration. The solution will overwrite the version during build-time with the commit hash from the git-commit-id-plugin. It uses the groovy-maven-plugin, so all other plugins especially the install and deploy plugin, will get the right version.

        <plugin>
          <!-- retrieve the current commit hash, for versioning the poms and artifacts -->
          <groupId>pl.project13.maven</groupId>
          <artifactId>git-commit-id-plugin</artifactId>
          <version>2.2.4</version>
          <executions>
            <execution>
              <phase>validate</phase>
              <goals>
                <goal>revision</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <skipPoms>false</skipPoms>
            <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
          </configuration>
        </plugin>
        <plugin>
          <!-- sets the version for each pom including children pom -->
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>versions-maven-plugin</artifactId>
          <version>2.5</version>
          <executions>
            <execution>
              <phase>process-sources</phase>
              <goals>
                <goal>set</goal>
              </goals>
              <inherited>false</inherited>
              <configuration>
                <!-- comes from the git-commit-id-plugin -->
                <newVersion>${git.commit.id}</newVersion>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <!-- this will change the version for the current build, so the artifacts will be installed and deployed with the corret version -->
          <groupId>org.codehaus.gmaven</groupId>
          <artifactId>groovy-maven-plugin</artifactId>
          <version>2.0</version>
          <executions>
            <execution>
              <phase>verify</phase>
              <goals>
                <goal>execute</goal>
              </goals>
              <configuration>
                <source>project.artifact.version='${git.commit.id}';</source>
              </configuration>
            </execution>
          </executions>
        </plugin>```

@TheSnoozer
Copy link
Collaborator

Hi, thanks for the additional input.
This plugin uses the MavenProject as well. Changing the artifact reference sounds like bad behaviour, but I check if this would change anything in any way....

@YoshiMan
Copy link

This would not overwrite any references. Only the version of the reference.
My comment before sounds like it would change a reference. But that's not what I meant.

@TheSnoozer
Copy link
Collaborator

Ohh I SEE....sorry for misunderstanding...what you said is basically another solution to achieve the same with the mvn install plugin....maven is such a pain sometimes...thanks for your suggestion and debugging session ;-)

@YoshiMan
Copy link

Yeah but my solution works for pom projects and multi-module-projects. So my solution is a better approach for my problem.

@bertramn
Copy link

bertramn commented Jan 8, 2018

The issues you are encountering here are due to maven evaluating the version tags well before firing any of your plugin code. You might want to check out the New'ish maven 3.5 revision and changelist feature, this could go really well with this plugin. But note that you will also have to "flatten" your pom file(s) before install and deploy otherwise they will unusable (as mentioned in the link above and painfully experienced in practice).

@TheSnoozer
Copy link
Collaborator

Thanks for the additional input....I can remember that with old maven versions you could set dynamic versions with properties where I was exactly using something along the lines <version>${revision}</version>. However maven removed it (even triggered ugly warnings) and it seems they are introducing it back again....well for me its now too late...I don't use maven anymore, but thanks for pointing it out - will probability only take until maven 8 until this is stable :P

@mariuszs
Copy link

mariuszs commented Mar 2, 2018

@bertramn This is nice idea, but flatten don't see any of maven-git-commit-id-plugin variables :(

mojohaus/flatten-maven-plugin#66 (comment)

@sangechen
Copy link

Hi @YoshiMan @TheSnoozer

blow is my version:

...
<version>${revision}</version>
...

        <plugin>
          <groupId>pl.project13.maven</groupId>
          <artifactId>git-commit-id-plugin</artifactId>
          <version>2.2.4</version>
          <executions>
            <execution>
              <id>get-the-git-infos</id>
              <phase>validate</phase>
              <goals>
                <goal>revision</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <injectAllReactorProjects>true</injectAllReactorProjects>
            <!-- use commit date as version: '$major.$minor.$incr' -->
            <dateFormat>yyyy.MM.dd</dateFormat>
            <!-- configure git-describe to produce '${abbr-sha1}' or '${abbr-sha1}-SNAPSHOT' -->
            <gitDescribe>
              <always>true</always>
              <dirty>-SNAPSHOT</dirty>
              <match>__null__</match>
            </gitDescribe>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.codehaus.gmavenplus</groupId>
          <artifactId>gmavenplus-plugin</artifactId>
          <version>1.6</version>
          <executions>
            <execution>
              <id>change-version</id>
              <phase>validate</phase>
              <goals>
                <goal>execute</goal>
              </goals>
              <configuration>
                <scripts>
                  <script><![CDATA[
                  import org.apache.maven.artifact.versioning.VersionRange;

                  git_revision = '${git.commit.time}-${git.commit.id.describe}'
                  if (! project.properties['revision']?.trim()) {
                      println 'Change `version` to ' + git_revision
                      System.properties['revision'] = git_revision
                      project.properties['revision'] = git_revision
                      project.properties['project.version'] = git_revision
                      project.properties['git.build.version'] = git_revision
                      project.version = git_revision
                      project.artifact.version = git_revision
                      project.artifact.versionRange = VersionRange.createFromVersion(git_revision)
                  }
                  ]]></script>
                </scripts>
              </configuration>
            </execution>

            <!-- for debug: -->
            <!--
              <execution>
              <id>dump-version</id>
              <phase>package</phase>
              <goals>
              <goal>execute</goal>
              </goals>
              <configuration>
              <scripts>
              <script><![CDATA[
              println System.properties['revision']
              println '${revision}'
              println '${project.version}'
              println '${git.build.version}'
              println project.version
              println project.artifact
              println project.artifact.version
              println project.artifact.versionRange
              ]]></script>
              </scripts>
              </configuration>
              </execution>
            -->
          </executions>
          <dependencies>
            <dependency>
              <groupId>org.codehaus.groovy</groupId>
              <artifactId>groovy-all</artifactId>
              <version>2.4.14</version>
              <scope>runtime</scope>
            </dependency>
          </dependencies>
        </plugin>

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>flatten-maven-plugin</artifactId>
          <version>1.0.1</version>
          <configuration>
            <!-- <flattenMode>ossrh</flattenMode> -->
            <flattenedPomFilename>.flattened-pom.xml</flattenedPomFilename>
            <outputDirectory>${project.build.directory}</outputDirectory>
            <updatePomFile>true</updatePomFile>
          </configuration>
          <executions>
            <execution>
              <id>flatten</id>
              <phase>initialize</phase>
              <goals>
                <goal>flatten</goal>
              </goals>
            </execution>
            <execution>
              <id>flatten.clean</id>
              <phase>clean</phase>
              <goals>
                <goal>clean</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

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

No branches or pull requests

9 participants