How the maven know exactly the ${jsf.version} in pom.xml - maven

In the pom.xml of our project, it define the dependencies for JSF as below:
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
My question is how/why Maven know exactly the version of JSF (in this case it get version 1.2) when we do not declare implicit JSF version?
Where do I can find we set the value of variable ${jsf.version}.

That's usually defined as a property in either the very same pom.xml, or in a parent pom.xml, or in user's own settings.xml, or in global settings.xml, or explicitly as command line argument.
In XML flavor, it look like this:
<properties>
<jsf.version>1.2</jsf.version>
</properties>
In command line flavor, it look like this:
mvn -Djsf.version=1.2
See also:
Maven POM Reference - Properties

Related

What does properties tag mean in pom.xml maven?

I am working in spring boot and I saw properties tag in pom.xml file but I don't understand what does it mean and what can we do in this tag.
properties tag for e.g.
<properties>
<java.version>1.8</java.version>
</properties>
What does properties tag mean ?
What can I add also in properties tag ?
What does properties tag mean ?
From Official Maven Docs :
Properties are the last required piece to understand POM basics. Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example:
In your case you have defined properties as version of java.
Now this property(java.version) can be reused later in maven pom file.
From Official Maven Docs :
They come in five different styles:
env.X: Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH
environment variable. Note: While environment variables themselves are
case-insensitive on Windows, lookup of properties is case-sensitive.
In other words, while the Windows shell returns the same value for
%PATH% and %Path%, Maven distinguishes between ${env.PATH} and
${env.Path}. The names of environment variables are normalized to all
upper-case for the sake of reliability.
project.x: A dot (.) notated path in the POM will contain the corresponding element's value. For example:
1.0 is accessible via
${project.version}.
settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example:
false is accessible via
${settings.offline}.
Java System Properties: All properties accessible via java.lang.System.getProperties() a-re available as POM properties,
such as ${java.home}.
x: Set within a element in the POM. The value of value may be used as
${someVar}.
What can i add also in properties tag ?
You can add all the variables which you need to reuse later in your maven pom file.
For e.g. Below POM snippet reuses jackson.version 4 times.
<properties>
<jackson.version>2.10.2</jackson.version>
<dropwizard.version>2.0.1</dropwizard.version>
<websocket.version>1.4.0</websocket.version>
<apachehttp.version>4.5.10</apachehttp.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apachehttp.version}</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>${websocket.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependencies>
References :
Maven Pom Properties

jetty version error when running mvn install

I have the below in my POM file:
<properties>
<main.basedir>${project.basedir}/..</main.basedir>
<jettyVersion>9.2.3.v20140905</jettyVersion>
<jersey.version>2.15</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
When I run mvn install getting this error with regards to versioning:
The following artifacts could not be resolved: org.glassfish.jersey.container:jersey-container-servlet:jar:9.2.3.v20140905
Correctly pointed by #Steve C in the comments, you seem to be using incorrect property name as the value of the version for jersey-container-servlet. The version as specified in the error jersey-container-servlet: jar:9.2.3.v20140905 doesn't exist as seen here. You can make sure the version used is either:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.15</version>
</dependency>
or use your defined property jersey.version as:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>

How can I use (refer) transitive dependency's version?

I'm facing a situation that I need to specify one of transitive dependency's version.
With following dependency tree,
A <- B <- C
I need to specify A's version in C's pom.xml
Is there any way to do this? So that I can filter some file like this?
A's version is #{...a.version}
If you want to specify the version of a (possible) transitive dependency, put the dependency into the dependencyManagement section of your POM. Then it is used if the dependency occurs transitively (and silently ignored if no such transitive dependency is found).
It is not possible to directly reference the version of some arbitrary dependency (transient or not).
However, in your parent pom you can define a property:
<properties>
...
<yourCdep.version>
...
</properties>
and add the dependency in to a dependencyManagement section:
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>yourCdep.group</groupId>
<artifactId>yourCdep</artifactId>
<version>${yourCdep.version}</version>
</dependency>
...
</dependencies>
</dependencyManagement>
Remove the version from the dependency in module B as it is now "managed".
The property value in the parent pom will be accessible in both modules A and B.
In particular, this property value can now be applied when resource filtering.
Use <optional>true</optional>:
C -> B -> A
pom(B) :
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>A</artifactId>
<version></version>
<optional>true</optional>
</dependency>
...
</dependencies>
pom(C):
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>B</artifactId>
<version></version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>A</artifactId>
<version></version>
</dependency>
...
</dependencies>

Dependencies vs DependenciesManagement?

I have gone through differences between dependencymanagement and dependencies in maven but i am still unclear when to use
just dependencies tag in parent pom and when to use dependenciesManagement tag ?
My understanding is when my all child modules need to use same dependency version then we should declare the Dependencies under Dependencies tag(without dependencyManagement tag)
But on other hand if some of the child project need to use different version then we should declare the Dependencies under Dependencies tag(which will be under dependencyManagement tag). then Child modules can refer them with overridden version
Is that correct ?
Declaring a <dependency> within <dependencyManagement> does not set the specified artifact as dependency for any project – parent or childs. It just states: If you want to use this as dependency then you can use it with these settings (version, scope, ...) without having to specify the settings again, and again, and ... You can, however, override a "management" setting in a "real" <dependency> anytime.
See also POM Reference, Dependency Management.
There are two options for a parent POM regarding your second paragraph:
As you describe correctly:
<dependencies>
<dependency>
<groupId>g-id</groupId>
<artifactId>a-id</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
I'd use this for consistency:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>g-id</groupId>
<artifactId>a-id</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>g-id</groupId>
<artifactId>a-id</artifactId>
</dependency>
</dependencies>
Your third paragraph is correct..

why don't use scope maven dependency Management?

if i define this dependency at parent's pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework<groupId>
<artifactId>spring-core<artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
and in one of the childss:
<project>
<dependencies>
<dependency> // note: no version declared
<groupId>org.springframework<groupId>
<artifactId>spring-core<artifactId>
</dependency>
so, the scope? is always compile, or can i specify?
compile is the default scope for a dependency (pom reference). You can specify scope also and it will be inherited

Resources