Skip to content

Commit e5bf457

Browse files
committed
Add documentation for swift-java resolve
1 parent 149358a commit e5bf457

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

Sources/SwiftJavaDocumentation/Documentation.docc/SwiftJavaCommandLineTool.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,89 @@ struct HelloSwiftMain: ParsableCommand {
199199

200200
### Download Java dependencies in Swift builds: swift-java resolve
201201

202-
> TIP: See the `Samples/DependencySampleApp` for a fully functional showcase of this mode.
202+
> TIP: See the `Samples/JavaDependencySampleApp` for a fully functional showcase of this feature.
203203
204-
TODO: documentation on this feature
204+
205+
The `swift-java resolve` command automates the process of downloading and resolving Java dependencies for your Swift project. This is configured through your `swift-java.config` file, where you can declare both the dependencies you need and the repositories from which to fetch them.
206+
207+
To get started, add a `dependencies` array to your configuration file, listing the Maven coordinates for each required library (e.g., `group:artifact:version`). You may also include a `repositories` array to specify custom Maven repositories. For example:
208+
209+
```json
210+
{
211+
"classes": {
212+
"org.apache.commons.io.FilenameUtils": "FilenameUtils",
213+
"org.apache.commons.io.IOCase": "IOCase",
214+
"org.apache.commons.csv.CSVFormat": "CSVFormat",
215+
"org.apache.commons.csv.CSVParser": "CSVParser",
216+
"org.apache.commons.csv.CSVRecord": "CSVRecord"
217+
},
218+
"dependencies": [
219+
"org.apache.commons:commons-csv:1.12.0"
220+
]
221+
}
222+
```
223+
224+
To resolve and download these dependencies, run:
225+
226+
```bash
227+
# See Samples/JavaDependencySampleApp/ci-validate.sh for a complete example
228+
swift-java resolve \
229+
swift-java.config \
230+
--swift-module JavaCommonsCSV \
231+
--output-directory .build/plugins/JavaCommonsCSV/destination/SwiftJavaPlugin/
232+
```
233+
234+
The tool will fetch all specified dependencies from the repositories listed in your config (or Maven Central by default), and generate a `swift-java.classpath` file. This file is then used for building and running your Swift-Java interop code.
235+
236+
If you do not specify any `repositories`, dependencies are resolved from Maven Central. To use a custom or private repository, add it to the `repositories` array, for example:
237+
238+
```json
239+
{
240+
"repositories": [
241+
{ "type": "maven", "url": "https://repo.mycompany.com/maven2" },
242+
{
243+
"type": "maven",
244+
"url": "https://repo2.mycompany.com/maven2",
245+
"artifactUrls": [
246+
"https://repo.mycompany.com/jars",
247+
"https://repo.mycompany.com/jars2"
248+
]
249+
},
250+
{ "type": "maven", "url": "https://secure.repo.com/maven2" },
251+
{ "type": "mavenLocal", "includeGroups": ["com.example.myproject"] },
252+
{ "type": "maven", "url": "build/repo" },
253+
{ "type": "mavenCentral" },
254+
{ "type": "mavenLocal" },
255+
{ "type": "google" }
256+
]
257+
}
258+
```
259+
260+
> Note: Authentication for private repositories is not currently handled directly by `swift-java`. If you need to access packages from a private repository that requires credentials, you can use Maven to download the required artifacts and then reference them via your local Maven repository in your configuration.
261+
262+
For practical usage, refer to `Samples/JavaDependencySampleApp` and the tests in `Tests/SwiftJavaTests/JavaRepositoryTests.swift`.
263+
264+
This workflow streamlines Java dependency management for Swift projects, letting you use Java libraries without manually downloading JAR files.
265+
266+
#### About the `classes` section
267+
268+
The `classes` section in your `swift-java.config` file specifies which Java classes should be made available in Swift, and what their corresponding Swift type names should be. Each entry maps a fully-qualified Java class name to a Swift type name. For example:
269+
270+
```json
271+
{
272+
"classes": {
273+
"org.apache.commons.io.FilenameUtils" : "FilenameUtils",
274+
"org.apache.commons.io.IOCase" : "IOCase",
275+
"org.apache.commons.csv.CSVFormat" : "CSVFormat",
276+
"org.apache.commons.csv.CSVParser" : "CSVParser",
277+
"org.apache.commons.csv.CSVRecord" : "CSVRecord"
278+
}
279+
}
280+
```
281+
282+
When you run `swift-java wrap-java` (or build your project with the plugin), Swift source files are generated for each mapped class. For instance, the above config will result in `CSVFormat.swift`, `CSVParser.swift`, `CSVRecord.swift`, `FilenameUtils.swift` and `IOCase.swift` files, each containing a Swift class that wraps the corresponding Java class and exposes its constructors, methods, and fields for use in Swift.
283+
284+
This mapping allows you to use Java APIs directly from Swift, with type-safe wrappers and automatic bridging of method calls and data types.
205285

206286
### Expose Swift code to Java: swift-java jextract
207287

0 commit comments

Comments
 (0)