Home > database >  In maven, can I define a variable used in another pom?
In maven, can I define a variable used in another pom?

Time:08-09

I'm getting an error when running maven build (unable to load a dependency).

[ERROR] Failed to execute goal on . . .

    Could not transfer artifact my.group:libme1:${someVariable} from/to . . .

I believe that the developer that published this artifact was supposed to be setting the variable ${someVariable} but didn't. I think this is a bug but I'm trying to work around it by setting the variable.

The POM for the JAR I'm depending on my.group:libme1:1.2.3 looks like this (snippet highlighting the issue):

<groupId>my.group</groupId>
<artifactId>libme1</artifactId>

<parent>
    <groupId>my.group</groupId>
    <artifactId>libme1-parent</artifactId>
    <version>${someVariable}</version>
</parent>

I tried defining it by adding -DsomeVariable=1.2.3 on the command line but it didn't work. For example, this command

mvn -DsomeVariable=1.2.3 clean install

should work based on Baeldung's article but doesn't.

Is there any way to set the variable so it can be used in another POM? I'm guessing this is not possible.

Searching for an answer I found:

The maven doc

If you know that this is bug, please let me know. I'm also reaching out to the publish of the artifact to ask them how this is supposed to work.

CodePudding user response:

Basically the dependency's pom is invalid, the reasoning is following:

maven allows developers to do following things:

  • define dependencies in parent pom
  • impose restrictions on dependencies via <dependencyManagement> in both current and parent pom
  • use placeholders ${...} in <version> element, which somehow get resolved via system properties and current/parent pom properties

all those features mentioned above are very convenient from development perspective, however when you publish artifacts those features cause a pain in behind: that became not possible to use external library without it's parent pom, because parent pom may define dependencies and properties.

In your particular case someone have define version of parent pom as ${someVariable}, that in turn means it is not possible to use that library without information about the value of ${someVariable}. However, even if you had known the "correct" value of ${someVariable} and might specify it via system properties, that would cause some weird behaviour: today you may specify one value for ${someVariable}, tomorrow you (or someone else) will specify another value and ultimately you will get different builds, due to that maven denies such configurations (that is much better to fail a build rather than build something unreliable), that would be wiser to initially deny publishing such poms, but we have what we have.

CodePudding user response:

It might be that the variable was stored in some user's settings.xml.

This would allow checking out an older version already in production for writing patches.

<settings>
  ...

  <profiles>
    <profile>
      <id>work-in-progress</id>
      <properties>
        <someVariable>1.2.3</someVariable>
      </properties>
    </profile>
  </profiles>
 
  <activeProfiles>
    <activeProfile>work-in-progress</activeProfile>
  </activeProfiles>
</settings>

So you might do that too. And search in users' directories, .m2 repo directories where usually the settings.xml is stored.

  • Related