Maven: Using an inherited property in a dependency classifier fails assembly

Given the three POM files:

  • C depends on B.
  • B inherits from A.
  • I can build A and B
  • C cannot be built due to its dependence on B.

The full source code and assembly output are provided below for your review.

Here is the POM:

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.foo</groupId> <artifactId>A</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>A</name> <repositories> <repository> <id>foo releases</id> <name>libs-releases-local</name> <layout>default</layout> <url>http://foo.net/artifactory/libs-releases-local</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.eclipse.swt</groupId> <artifactId>swt</artifactId> <classifier>${swt.classifier}</classifier> <version>3.6.1</version> </dependency> </dependencies> <profiles> <profile> <id>windows-x86</id> <properties> <swt.classifier>win32-x86</swt.classifier> </properties> </profile> </profiles> </project> 

Here is the B POM:

 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.foo</groupId> <artifactId>A</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../A</relativePath> </parent> <artifactId>B</artifactId> <packaging>jar</packaging> <name>B</name> <profiles> <profile> <id>windows-x86</id> <properties> <swt.classifier>win32-x86</swt.classifier> </properties> </profile> </profiles> </project> 

Here is the C POM:

 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.foo</groupId> <artifactId>C</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>C</name> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>B</artifactId> <version>${project.version}</version> </dependency> </dependencies> </project> 

Here is the build result from C:

 ------------------------------------------------------------------------ Building C task-segment: [install] ------------------------------------------------------------------------ [compiler:compile] Nothing to compile - all classes are up to date Downloading: http://foo.net/artifactory/libs-releases-local/org/eclipse/swt/swt/3.6.1/swt-3.6.1-${swt.classifier}.jar [WARNING] Unable to get resource 'org.eclipse.swt:swt:jar:${swt.classifier}:3.6.1' from repository foo releases (http://foo.net/artifactory/libs-releases-local): Error transferring file: foo.net Downloading: http://repo1.maven.org/maven2/org/eclipse/swt/swt/3.6.1/swt-3.6.1-${swt.classifier}.jar Unable to find resource 'org.eclipse.swt:swt:jar:${swt.classifier}:3.6.1' in repository central (http://repo1.maven.org/maven2) ------------------------------------------------------------------------ [ERROR]BUILD ERROR ------------------------------------------------------------------------ Failed to resolve artifact. Missing: ---------- 1) org.eclipse.swt:swt:jar:${swt.classifier}:3.6.1 

I know this problem is related to https://issues.apache.org/jira/browse/MNG-3228 , but I'm not sure how to fix it. Please, help!

UPDATE

It helped to add a classifier to B. Now C builds while the repository contains only the B jar file. If I upload the B POM file along with the JAR to the repository, C will fail with the error above ( ${swt.classifier }). Any ideas?

+4
source share
4 answers

This is not possible with Maven 3.1.0. Here is the corresponding function request: https://issues.apache.org/jira/browse/MNG-1388

+1
source

In a comment you write: β€œI expect that the SWT classifier will be resolved at assembly time B and not at assembly time C”, but this is wrong - you need the classifier at time C because C has a dependency on swt (transitive via A ) This dependence is completely determined only by the property, so you should have a way to evaluate the property in C pom.

  • A depends on swt - $ {classifier}
  • C depends on A
  • therefore C depends on swt - $ {classifier}
  • therefore, C pom must define a property. It can be defined by a profile (as in A) or manually at runtime (bad for reproducibility), but you cannot build C without it.

It's just as simple (and mysterious).

If you expect the property to somehow completely β€œresolve” along the way and be well defined by the time you create C, you don’t understand how Maven handles these properties. This leaves them alone. There was an attempt to do some different things in Maven 2.1 (the expression of the classifier property would be converted to its value when you set A), but this was unsuccessful, caused a lot of amazing behaviors, it was canceled by 2.2, and actually was called 2.1 to be quickly outdated. For more information and some tips on how difficult the problem really is, see the link below.

https://cwiki.apache.org/confluence/display/MAVENOLD/Artifact-Coordinate+Expression+Transformation

Unless Maven developers decide otherwise, I think we will continue the behavior that has been since 2.0: "Expressions in the coordinates of the artifact are ignored. Users have a lot of rope with which to hang yourself"

Once you get used to it, it no longer bothers. This is only when you try to invade Maven to be surprised.

+2
source

Maven tries to find the artifact org.eclipse.swt: swt: 3.6.1, but the coordinates will not be resolved correctly. The error indicates that $ {swt.classifier} is not recognized from the <properties/> block in your POM.xml. Since this value is displayed in the <profile/> block, can you check which Maven command you are using?

Try the following: mvn dependency:resolve -P windows-x86

Also, make sure that the SWT version and classifier are indeed correct. The latest version that I see on Maven Central is not 3.6.0, but 3.3.0-v3346

+1
source

I know this problem is related to https://issues.apache.org/jira/browse/MNG-3228 , but I'm not sure how to fix it.

I am not sure that there is a link to this problem, I do not see anything related to profile activation in the pom.xml shown above.

Actually, I'm not even sure to understand the expected result. Where should the classifier come from? I may be missing some parts, but I think you should install / deploy a qualified version of B (with POM fully enabled) and have C depending on this qualified version.

How do I need to change the B POM to deploy a qualified version? I expect the SWT classifier to be resolved at build time B, not build time C.

Yes, but at the time of building C, C requires B and B, so the installed / deployed .pom of B must be fully enabled. At least the way I think it can work.

But I have to admit that I'm not sure how to handle this case correctly, and after reading issues like the MNG-4140 or the artifact-coordinate conversion page , I'm completely confused.

I suggest posting this to the maven user list for the right path (and I will keep a close eye on the thread, because I think I have some broken POMs using profiles, properties and dependencies to fix now, thanks :)

0
source

All Articles