|
1 | | - |
2 | 1 | <!-- |
3 | 2 | { |
4 | 3 | "title": "JavaSettings in Application.cfc, Components and CFConfig.json", |
|
23 | 22 | } |
24 | 23 | --> |
25 | 24 |
|
26 | | -# Java Settings in Application.cfc (Now with Maven Support) |
27 | | - |
28 | | -This document provides information about configuring Java settings in Lucee using `Application.cfc`, and other new places like `.CFConfig.json`, `createObject`, and components. |
29 | | - |
30 | | -## Introduction |
| 25 | +# Java Settings |
31 | 26 |
|
32 | | -The `this.javasettings` settings in `Application.cfc` allow you to define Java library paths, OSGi bundle paths, Maven libraries, and other Java-related configurations. These settings cannot be configured through the Lucee admin or environment variables. |
| 27 | +The `javasettings` configuration tells Lucee where to find Java libraries. You can load JARs from local paths, pull dependencies from Maven automatically, or use OSGi bundles for advanced modular deployments. |
33 | 28 |
|
34 | | -Starting with Lucee 6.2, we’ve extended Java settings to support Maven libraries and expanded their use in additional contexts, including `.CFConfig.json`, `createObject`, and individual components. |
| 29 | +Configure it in `Application.cfc` (app-wide), on individual components (isolated), in `.CFConfig.json` (server-wide), or inline with `createObject`. |
35 | 30 |
|
36 | | -## Configuring Java Settings |
| 31 | +## Application.cfc |
37 | 32 |
|
38 | | -You can configure Java settings in `Application.cfc`, `.CFConfig.json`, within components, or using the `createObject` function. |
39 | | - |
40 | | -### Load Paths |
41 | | - |
42 | | -You can define regular JARs (not OSGi) by specifying the path to a directory or the JAR file itself. |
| 33 | +The most common approach - makes libraries available to your entire application: |
43 | 34 |
|
44 | 35 | ```cfml |
45 | | -this.javasettings.loadPaths = [ |
46 | | - "/my/local/path/to/whatever/lib/", |
47 | | - "/my/local/path/to/whatever/lib/xyz.jar" |
48 | | -]; |
| 36 | +this.javasettings = { |
| 37 | + "maven": [ |
| 38 | + { |
| 39 | + "groupId": "org.quartz-scheduler", |
| 40 | + "artifactId": "quartz", |
| 41 | + "version": "2.3.2" |
| 42 | + } |
| 43 | + ], |
| 44 | + "loadPaths": [ |
| 45 | + "/my/local/path/to/libs/", |
| 46 | + "/my/local/path/to/libs/example.jar" |
| 47 | + ], |
| 48 | + "reloadOnChange": true, |
| 49 | + "watchInterval": 60, |
| 50 | + "watchExtensions": ["jar", "class"] |
| 51 | +}; |
49 | 52 | ``` |
50 | 53 |
|
51 | | -### Bundle Paths |
| 54 | +## Maven Dependencies (6.2+) |
52 | 55 |
|
53 | | -You can load local OSGi bundles in a similar way. |
| 56 | +Pull libraries from Maven Central automatically - no manual JAR management: |
54 | 57 |
|
55 | 58 | ```cfml |
56 | | -this.javasettings.bundlePaths = [ |
57 | | - "/my/local/path/to/whatever/lib/", |
58 | | - "/my/local/path/to/whatever/lib/xyz.jar" |
| 59 | +this.javasettings.maven = [ |
| 60 | + { |
| 61 | + "groupId": "org.quartz-scheduler", |
| 62 | + "artifactId": "quartz", |
| 63 | + "version": "2.3.2" |
| 64 | + }, |
| 65 | + { |
| 66 | + "groupId": "commons-beanutils", |
| 67 | + "artifactId": "commons-beanutils", |
| 68 | + "version": "1.9.4" |
| 69 | + } |
59 | 70 | ]; |
60 | 71 | ``` |
61 | 72 |
|
62 | | -### Maven Support |
| 73 | +### Shorthand Syntax |
63 | 74 |
|
64 | | -Starting in Lucee 6.2, Maven support has been added to `this.javasettings`, allowing you to define Maven dependencies directly. Maven manages libraries by automatically handling their retrieval and versioning. |
| 75 | +Use `group:artifact:version` strings for brevity: |
65 | 76 |
|
66 | 77 | ```cfml |
67 | 78 | this.javasettings.maven = [ |
68 | | - { |
69 | | - "groupId": "org.quartz-scheduler", |
70 | | - "artifactId": "quartz", |
71 | | - "version": "2.3.2" |
72 | | - }, |
73 | | - { |
74 | | - "groupId": "commons-beanutils", |
75 | | - "artifactId": "commons-beanutils", |
76 | | - "version": "1.9.4" |
77 | | - } |
| 79 | + "org.quartz-scheduler:quartz:2.3.2", |
| 80 | + "commons-beanutils:commons-beanutils:1.9.4" |
78 | 81 | ]; |
79 | 82 | ``` |
80 | 83 |
|
81 | | -If the `version` is omitted, Lucee will use the latest available version of the Maven artifact. |
| 84 | +Omit `version` for latest (e.g., `"commons-beanutils:commons-beanutils"`). |
82 | 85 |
|
83 | | -## Java Settings in `.CFConfig.json` |
| 86 | +## Load Paths |
84 | 87 |
|
85 | | -You can also define Java settings globally for all applications through the `.CFConfig.json` configuration file. This is especially useful when managing Docker environments. |
| 88 | +Point to local JAR files or directories containing JARs: |
86 | 89 |
|
87 | | -```json |
88 | | -{ |
89 | | - "javasettings": { |
90 | | - "maven": [ |
91 | | - { |
92 | | - "groupId": "org.quartz-scheduler", |
93 | | - "artifactId": "quartz", |
94 | | - "version": "2.3.2" |
95 | | - }, |
96 | | - { |
97 | | - "groupId": "commons-beanutils", |
98 | | - "artifactId": "commons-beanutils", |
99 | | - "version": "1.9.4" |
100 | | - } |
101 | | - ], |
102 | | - "loadPaths": [ |
103 | | - "/my/local/path/to/whatever/lib/", |
104 | | - "/my/local/path/to/whatever/lib/xyz.jar" |
105 | | - ], |
106 | | - "bundlePaths": [ |
107 | | - "/my/local/path/to/whatever/lib/", |
108 | | - "/my/local/path/to/whatever/lib/xyz.jar" |
109 | | - ] |
110 | | - } |
111 | | -} |
| 90 | +```cfml |
| 91 | +this.javasettings.loadPaths = [ |
| 92 | + "/my/local/path/to/whatever/lib/", |
| 93 | + "/my/local/path/to/whatever/lib/xyz.jar" |
| 94 | +]; |
112 | 95 | ``` |
113 | 96 |
|
114 | | -### Using Java Settings in `Application.cfc` |
115 | | - |
116 | | -In your [[tag-Application]], you can define or override Java settings specific to your application. |
| 97 | +## Bundle Paths |
117 | 98 |
|
118 | | -This is the primary way to configure Java dependencies at the application level. |
| 99 | +For OSGi bundles (modular JARs with explicit dependency metadata). Most applications don't need this - use `loadPaths` for regular JARs: |
119 | 100 |
|
120 | 101 | ```cfml |
121 | | -this.javasettings = { |
122 | | - "maven": [ |
123 | | - { |
124 | | - "groupid": "commons-beanutils", |
125 | | - "artifactid": "commons-beanutils", |
126 | | - "version": "1.9.4" |
127 | | - } |
128 | | - ], |
129 | | - "loadPaths": [ |
130 | | - "/my/local/path/to/libs/", |
131 | | - "/my/local/path/to/libs/example.jar" |
132 | | - ], |
133 | | - "bundlePaths": [ |
134 | | - "/my/local/path/to/bundles/" |
135 | | - ], |
136 | | - "reloadOnChange": true, |
137 | | - "watchInterval": 60, |
138 | | - "watchExtensions": ["jar", "class"] |
139 | | -}; |
| 102 | +this.javasettings.bundlePaths = [ |
| 103 | + "/my/local/path/to/whatever/lib/", |
| 104 | + "/my/local/path/to/whatever/lib/xyz.jar" |
| 105 | +]; |
140 | 106 | ``` |
141 | 107 |
|
142 | | -This method gives you flexibility to handle specific Java dependencies within each application, ensuring that classloaders are configured per application. |
143 | | - |
144 | | -### Using Java Settings in a Component |
| 108 | +## Component-Level |
145 | 109 |
|
146 | | -Maven dependencies and other Java settings can also be defined as part of a [[tag-component]]. |
147 | | - |
148 | | -This ensures that only the classes loaded within that component will use the specified settings, isolating it from the rest of the application and avoiding conflicts. |
| 110 | +Isolate libraries to a single component. The attribute value must be a JSON string: |
149 | 111 |
|
150 | 112 | ```cfml |
151 | | -component javaSettings = '{ |
152 | | - "maven": [ |
153 | | - { |
154 | | - "groupId": "commons-beanutils", |
155 | | - "artifactId": "commons-beanutils", |
156 | | - "version": "1.9.4" |
157 | | - } |
158 | | - ] |
| 113 | +component javasettings='{ |
| 114 | + "maven": ["commons-beanutils:commons-beanutils:1.9.4"] |
159 | 115 | }' { |
160 | | - // Component logic |
| 116 | + // Only this component has access to beanutils |
161 | 117 | } |
162 | 118 | ``` |
163 | 119 |
|
164 | | -This method is useful for encapsulating components with specific versions of libraries, preventing conflicts with other parts of the application. |
| 120 | +See [[java-libraries]] for more on using Maven dependencies in components. |
165 | 121 |
|
166 | | -### Using Java Settings in `createObject` |
| 122 | +## createObject |
167 | 123 |
|
168 | | -Java settings can also be defined dynamically when creating Java objects using the [[function-createObject]] function. |
| 124 | +Load a library inline when creating a Java object: |
169 | 125 |
|
170 | 126 | ```cfml |
171 | | -createObject("java", "org.apache.commons.beanutils.BeanUtils", { |
172 | | - "maven": [ |
173 | | - { |
174 | | - "groupId": "commons-beanutils", |
175 | | - "artifactId": "commons-beanutils", |
176 | | - "version": "1.9.4" |
177 | | - } |
178 | | - ] |
| 127 | +obj = createObject( "java", "org.apache.commons.beanutils.BeanUtils", { |
| 128 | + "maven": ["commons-beanutils:commons-beanutils:1.9.4"] |
179 | 129 | }); |
180 | 130 | ``` |
181 | 131 |
|
182 | | -This approach provides the flexibility to load Java classes and dependencies at runtime. |
183 | | - |
184 | | -## Classloader Recycling |
185 | | - |
186 | | -Lucee automatically generates a unique hash based on the defined Java settings and maintains a pool of corresponding classloaders. This means that classloaders are reused efficiently, reducing resource consumption and improving performance. |
187 | | - |
188 | | -## Reload On Change |
189 | | - |
190 | | -The setting `this.javasettings.reloadOnChange` indicates whether to reload updated classes and JARs dynamically, without restarting Lucee. The default value is `false`. |
191 | | - |
192 | | -```cfml |
193 | | -this.javasettings.reloadOnChange = false; |
194 | | -``` |
195 | | - |
196 | | -## Watch Interval |
| 132 | +## .CFConfig.json |
197 | 133 |
|
198 | | -The setting `this.javasettings.watchInterval` defines the interval in seconds that Lucee looks for changes. The default value is `60`. |
| 134 | +Server-wide settings in your CFConfig file: |
199 | 135 |
|
200 | | -```cfml |
201 | | -this.javasettings.watchInterval = 60; |
| 136 | +```json |
| 137 | +{ |
| 138 | + "javasettings": { |
| 139 | + "maven": [ |
| 140 | + "org.quartz-scheduler:quartz:2.3.2", |
| 141 | + "commons-beanutils:commons-beanutils:1.9.4" |
| 142 | + ], |
| 143 | + "loadPaths": [ |
| 144 | + "/my/local/path/to/libs/" |
| 145 | + ] |
| 146 | + } |
| 147 | +} |
202 | 148 | ``` |
203 | 149 |
|
204 | | -## Watch Extensions |
205 | | - |
206 | | -The setting `this.javasettings.watchExtensions` defines the extensions Lucee looks for when you list a directory with `loadPaths` or `bundlePaths`. The default value is `["jar", "class"]`. |
207 | | - |
208 | | -```cfml |
209 | | -this.javasettings.watchExtensions = ["jar", "class"]; |
210 | | -``` |
| 150 | +## Additional Settings |
211 | 151 |
|
212 | | -## Conclusion |
| 152 | +- `reloadOnChange` - Reload updated classes/JARs without restart (default: `false`) |
| 153 | +- `watchInterval` - Seconds between change checks (default: `60`) |
| 154 | +- `watchExtensions` - Extensions to watch (default: `["jar", "class"]`) |
213 | 155 |
|
214 | | -With the introduction of Maven support and the ability to define Java settings in multiple contexts such as `.CFConfig.json`, `Application.cfc`, components, and `createObject`, Lucee provides enhanced flexibility for integrating and managing Java libraries. These features help avoid conflicts and ensure that your Java dependencies are managed efficiently across various parts of your application. |
| 156 | +Lucee pools classloaders based on settings hash for efficient reuse. |
0 commit comments