1

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.

I also ran:

mvn -DsomeVariable=1.2.3 help:effective-pom

and I see the variable being set, so I know he POM I'm using has that defined, but for some reason another POM doesn't pick up that value (or that is how it appears to me).

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.

PatS
  • 8,833
  • 12
  • 57
  • 100
  • As far as i know you can’t use properties in the parent.version (besides a few exceptions). Can you share the full name of the property? See for example https://stackoverflow.com/questions/10582054/maven-project-version-inheritance-do-i-have-to-specify-the-parent-version – slindenau Aug 09 '22 at 05:32
  • One more related post, see my comment there as well: https://stackoverflow.com/questions/73183297/maven-parent-pom-version-declaration-in-child-pom-dynamically#comment129271971_73183297 – slindenau Aug 10 '22 at 10:44

2 Answers2

2

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.

Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18
  • `use placeholders ${...} in element`; yes, but *not* (except for a few exceptions) in the parent.version of your current pom.xml. Exactly for the reasons you elaborate in this answer. See the comments i left on the question. – slindenau Aug 24 '22 at 15:18
  • @slindenau that is irrelevant for **published** artifacts – Andrey B. Panfilov Aug 24 '22 at 15:52
  • Could it be that the specific library used the revision property, but forgot to apply the flatten plugin as per the [documentation](https://maven.apache.org/maven-ci-friendly.html)? How else would one be able to publish a maven artifact that can not be built by maven? – slindenau Aug 24 '22 at 17:59
  • 1
    @slindenau I do believe that was deployed by either very old maven version or, like you suspected, that was missing fatten plugin. Other cases like `gradle` or manual call of `deploy:deploy-file` are less probable: nobody cares about parent poms when using `gradle` or `deploy:deploy-file` – Andrey B. Panfilov Aug 25 '22 at 06:33
1

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I tried setting the variable using `-DsomeVariable=1.2.3` on the command line and `mvn help:effective-pom` showed the variable set in a `` block and it still was not picked up by the other POM/dependency I was using. Also my belief is that setting it in my `.m2/settings.xml` file will do the same thing (as setting it via the command line) and it won't fix the problem. **But I will give this a try.** – PatS Aug 09 '22 at 16:37