Project

General

Profile

How to Access a Maven Repository » History » Version 27

Henning Blohm, 02.09.2025 08:37

1 19 Henning Blohm
h1. Maven Repos Support (MVNCR)
2 1 Henning Blohm
3 19 Henning Blohm
(#1696)
4 1 Henning Blohm
5 17 Henning Blohm
Maven repository can be integrated with Z2 as component repositories.
6 1 Henning Blohm
7 20 Henning Blohm
This is used for most of the [[Samples]] and [[Add-ons]] to access third-party binaries, so these are good examples. See [[Sample-hibernate-basic#How-this-sample-makes-use-of-MVNCR]] for a discussion on use of the MVNCR.
8 18 Henning Blohm
9
For Add-ons it is typical to rely on fragments (see below) to declare dependencies, and for a sample (or - of course - any actual solution) it is typical to declare a maven component repository to set the actual remote source of artifacts (and more).
10
11 5 Henning Blohm
h2. Principles
12 1 Henning Blohm
13 5 Henning Blohm
The main idea is that based on some root artifacts and some maven remote repository configuration, jar artifacts and dependencies will be made available as Java component in Z2 that can be referenced or included as suits best.
14
15
h2. Name mapping and version resolution
16
17 1 Henning Blohm
Artefacts in Maven repos have a fully qualified name of the form 
18
19
<pre>
20
<groupId>:<artifactId>:<version>
21
</pre>
22
23 27 Henning Blohm
or fully qualified, using a gradle-style syntax:
24 1 Henning Blohm
25 5 Henning Blohm
<pre>
26 27 Henning Blohm
<groupId>:<artifactId>:<version>:<classifier>@<packaging>
27 5 Henning Blohm
</pre>
28 1 Henning Blohm
29 27 Henning Blohm
Partial naming is possible. E.g. 
30 1 Henning Blohm
31
<pre>
32 27 Henning Blohm
com.atomikos:transactions-jta:6.0.0:jakarta
33
</pre>
34
35
For the 6.0.0 version of the Atomikos @transaction-jta@ library with "jakarta" classifier.
36
37
By default, an artifact @<groupId>:<artifactId>:<version>@ with "jar" packaging will result into a Java component of name 
38
39
<pre>
40 1 Henning Blohm
<groupId>:<artifactId>/java
41 27 Henning Blohm
</pre>
42
43
independently of its classifier. I.e. @com.atomikos:transactions-jta/java@ (or @com.atomikos:transactions-jta@ for short) in the case of the example above.
44
45
Any other packaging will lead to 
46
47
<pre>
48
<groupId>:<artifactId>/<packaging>
49 1 Henning Blohm
</pre>
50
51 5 Henning Blohm
Given that the resolution of the root artifacts and dependencies lead to artifacts of the same packaging, group id, and artifact id but with different versions the higher version number will be used (but see #1695).
52 1 Henning Blohm
53 5 Henning Blohm
h2. Example configuration
54
55
A maven repository component may look like this:
56
57 26 Henning Blohm
<pre><code class="ruby">
58
#
59
# we want to part of it
60
#
61
com.zfabrik.systemStates.participation=com.zfabrik.boot.main/process_up
62
#
63
# but only when all other repos are up
64
#
65
com.zfabrik.systemStates.dependency=com.zfabrik.boot.main/sysrepo_up
66
#
67
# it's a maven repo
68
#
69 5 Henning Blohm
com.zfabrik.component.type=com.zfabrik.mvncr
70 9 Henning Blohm
mvncr.priority=200
71
mvncr.roots=\
72
	org.springframework:spring-context:4.0.2.RELEASE,\
73 5 Henning Blohm
	org.springframework:spring-aspects:4.0.2.RELEASE,\
74
	org.springframework:spring-tx:4.0.2.RELEASE,\
75
	org.springframework:spring-orm:4.0.2.RELEASE,\
76
	org.springframework:spring-web:4.0.2.RELEASE,\
77
	org.springframework.security:spring-security-core:3.2.2.RELEASE,\
78
	org.springframework.security:spring-security-web:3.2.2.RELEASE,\
79
	org.springframework.security:spring-security-config:3.2.2.RELEASE,\
80 9 Henning Blohm
	org.springframework.security:spring-security-aspects:3.2.2.RELEASE,\
81 1 Henning Blohm
	org.hibernate:hibernate-entitymanager:4.3.4.Final,\
82
	aopalliance:aopalliance:1.0,\
83
	org.aspectj:aspectjweaver:1.7.4,\
84 9 Henning Blohm
	org.aspectj:aspectjtools:1.6.9,\
85
86
mvncr.excluded=\
87
        org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec
88 1 Henning Blohm
89 9 Henning Blohm
mvncr.managed=\
90
        commons-logging:commons-logging:1.1.2
91 26 Henning Blohm
</code></pre>
92 5 Henning Blohm
 
93 1 Henning Blohm
By default, all non-optional compile scope dependencies will be resolved. The resulting Java component will have the target artifact as API library and all non-optional compile scope dependencies as public references in their mapped form.
94
95
The z2 core will use lazy component class loaders to make sure that use of include libraries has virtually no runtime penalty.
96 26 Henning Blohm
97
*NOTE* the state participation and dependency declaration. We want to make sure that other repositories, like file system, dev repo, git or subversion repositories are known to the system before we start maven resolution. We ask all other repositories to participate in sysrepo_up so that the declaration for the maven repository above makes sure this is happening. The reason this is important is that maven fragments (see below) may be supplied by other repositories. We want to make sure that all fragments are visible before the repository implementation commences dependency resolution.
98
99 5 Henning Blohm
100 13 Henning Blohm
h2. Component properties
101 6 Henning Blohm
102 13 Henning Blohm
|_. name |_. meaning |_. default |
103
| mvncr.settings | Specifies the location of the settings XML file relative to the components resources | settings.xml |
104
| mvncr.roots | A comman-separated list of root artifacts. See below for more details | n.a. |
105
| mvncr.priority | The repository priority in repository chaining as defined in IComponentsRepository | 500 |
106 24 Henning Blohm
| mvncr.managed	| Fixed artifact versions, if encountered during recursive root resolution. This corresponds to a <dependencyManagement> section in a Maven POM file | n.a. |
107 13 Henning Blohm
| mvncr.excluded | A comma separated list of artifacts that will be skipped during resolution of any root | n.a. | 
108
109 5 Henning Blohm
h2. Fine-Tuning of the Dependency Graph
110 9 Henning Blohm
111 5 Henning Blohm
In order to deviate from the default resolution and mapping, maven repository roots may be specified with a query string like this:
112 1 Henning Blohm
113
<pre>
114 5 Henning Blohm
	org.springframework.security:spring-security-aspects:3.2.2.RELEASE?versioned=true&scope=RUNTIME&excluded=commons-logging:commons-logging:1.1.2
115 1 Henning Blohm
</pre>
116
117 13 Henning Blohm
Admissable query params are 
118 1 Henning Blohm
119 13 Henning Blohm
|_. param |_. meaning |
120
| versioned | If set to @true@, the version part will not be removed from the java component name mapping and instead a versioned name is used. That is, in the case above, a java component @org.springframework.security:spring-security-aspects:3.2.2.RELEASE/java@ would be mapped. This is useful if "non-default" versions are required. |
121
| scope | Any of RUNTIME, COMPILE, PROVIDED, SYSTEM, TEST. Corresponds to the corresponding Maven dependency scopes. If set, non-optional dependencies of the respective scope will be traversed to resolve dependencies. |
122
| excluded | Exclusions on the dependency graph. |
123 6 Henning Blohm
124
h2. Handling the impact of PROVIDED references
125
126
At times, Maven provided artifacts require the presence of artifacts on the classpath that are expected to be required by the environment - most typically this is true for Web framework w.r.t the Servlet API.
127 8 Henning Blohm
128 1 Henning Blohm
In that case, it is simplest to create a Z2 project representation that includes all related artifacts (or include directly). In order to simplify this include process, includes may be specified to be transitively following Java component references by adding a corresponding query modifier to the include definition:
129
130
<pre>
131
java.publicIncludes=\
132 6 Henning Blohm
	com.vaadin:vaadin-server?expand=true
133 9 Henning Blohm
</pre> 
134 12 Henning Blohm
135 14 Henning Blohm
h2. Modularising Maven Dependencies
136
137 21 Henning Blohm
At times, it is useful to not have all required dependency roots in one component declaration but rather allow some modularization-friendly spread out declaration of dependency roots within a system. This is implemented by the *fragment* approach.
138
139
For example, an addon may define additional references, but in order to be reusable, it should not dictate what source to resolve those dependencies from. For example, this "mvnFragment":https://redmine.z2-environment.net/projects/z2-addons/repository/revisions/master/entry/com.zfabrik.springframework/mvnFragment.properties is the fragment of the [[Spring Add-on]]:
140
141
<pre>
142
#
143
# A fragment adding Maven dependencies
144
#
145
com.zfabrik.component.type=com.zfabrik.mvncr.fragment
146
147
#
148
# adds to default repository
149
#
150
151
mvncr.component=\
152
   environment/mavenDefault
153
154
mvncr.roots=\
155
	org.springframework:spring-context-support:4.0.2.RELEASE,\
156
	org.springframework:spring-context:4.0.2.RELEASE,\
157
	org.springframework:spring-aspects:4.0.2.RELEASE,\
158
	org.springframework:spring-tx:4.0.2.RELEASE,\
159
	org.springframework:spring-orm:4.0.2.RELEASE,\
160 23 Henning Blohm
        (....)
161 21 Henning Blohm
</pre>
162
163 22 Henning Blohm
Fragments are useful beyond separation of dependency and source repository separation. As conflict resolution will always happen within the scope of *one* Maven component repository, merging roots from different fragments for one repository is more consistent (in terms of conflict resolution) than having different maven repository declaration. In fact, in most cases having one Maven Component Repository will be the only manageable approach.
164 14 Henning Blohm
165
In order to add a _fragment_ to a Maven Component Repository declare a component of type *com.zfabrik.mvncr.fragment* with the following properties
166
167
|_. name |_. meaning |_. default |
168 16 Henning Blohm
| mvncr.component | The component name (or a comma-separated list of component names) of MVNCR declarations this fragment adds to or a fragment adding to some other mvncr | n.a. |
169 14 Henning Blohm
| mvncr.roots | As above. Will be merged with the repo this fragment adds to | n.a. |
170 25 Henning Blohm
| mvncr.managed	| As above. Will be merged with the repo this fragment adds to | n.a. |
171
| mvncr.excluded | As above. Will be merged with the repo this fragment adds to | n.a. |
172 14 Henning Blohm
173
174 12 Henning Blohm
h2. Development Support
175
176
When running in Development mode, the repository will also provide the source (classifier) artifact if available, so that the Eclipsoid plugins will provide source code attachments to the development environment whenever possible during classpath resolution. 
177 9 Henning Blohm
178
h2. References
179 10 Henning Blohm
180 7 Henning Blohm
* Main Javadoc: "MvnRepositoryResource":http://www.z2-environment.net/javadoc/com.zfabrik.mvncr!2Fjava/impl/com/zfabrik/impl/mvncr/MvnRepositoryResource.html
181
182
h2. To Dos
183
184 1 Henning Blohm
# Handling of SNAPSHOT-Versions to be revisited