Ivy's Multiple Resolvers

本文介绍如何使用Ivy通过多个解析器检索模块,包括本地和Maven2仓库的配置方法。演示了如何创建解析器链来解决依赖项,并提供了一个简单的项目示例,展示了如何设置和运行该项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This example is an illustration of how modules can be retrieved by multiple resolvers. Using multiple resolvers can be useful in many contexts, here are some examples:

  • separate integration builds from releases
  • use a public repository for third party modules and a private one for internal modules
  • use a repository for storing modules which are not accurate in an unmanaged public repository
  • use a local repository to expose builds made on one developer's station

In Ivy, the use of multiple resolvers is supported by one compound resolver called a chain resolver.

In our example, we will simply show how to use two resolvers, one on a local repository and one using maven2 repository.

project description

the project: chained-resolvers

The project is very simple and contains only one simple class: example.Hello.

It depends on two libraries: Apache commons-lang and a little test library (sources are included in jar file). The test library is used by the project to uppercase a string, and commons-lang is used to capitalize the same string.

Here is the content of the project:

  • build.xml: the ant build file for the project
  • ivy.xml: the ivy project file
  • src\example\Hello.java: the only class of the project

Let's have a look at the ivy.xml file:

<ivy-module version="1.0">
<info organisation="org.apache" module="chained-resolvers"/>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.0"/>
<dependency name="test" rev="1.0"/>
</dependencies>
</ivy-module>

As we expect, the ivy file declares to be dependent on the two libraries that the project use: commons-lang and test. Note that we don't specify the organisation for the dependency test, in this case Ivy assumes the same org as the declaring module, ie org.apache in this case.

the ivy settings

The ivy settings is made in the settings directory it contains only one file: ivysettings.xml.

<ivysettings>
<settings defaultResolver="chain-example"/>
<resolvers>
<chain name="chain-example">
<filesystem name="libraries">
<artifact pattern="${ivy.settings.dir}/repository/[artifact]-[revision].[ext]" />
</filesystem>
<ibiblio name="ibiblio" m2compatible="true" />
</chain>
</resolvers>
</ivysettings>

the settings tag

This tag initializes ivy with some parameters. Here only one is used, the name of the resolver to use by default.

the resolvers tag

Under this tag, we can find the description of the resolvers that ivy will use. In our example, we have only one resolver, called "chain-example", which is quite special as it defines a list (a chain) of resolvers.
The resolvers put in the chain are :

  • libraries : it is a file resolver. This one is configured to look for artifacts in the "repository" sub directory of the directory that contains the ivysettings.xml file.
  • ibiblio : this resolver is a special one. It looks in the ibiblio maven repository to retrieve the libraries.

That's it, we have configured a chain of resolvers!

walkthrough

step 1: preparation

Open a DOS or shell window, and go to the "chained-resolvers" directory.

step 2: clean directory tree

On the prompt type: ant
This will clean up the entire project directory tree and ivy cache. You can do it each time you want to clean up this example.
In almost all examples, we provide a clean target as default target. Since most examples use the same Ivy cache, you will clean the whole Ivy cache each time you call this target.

Cleaning Ivy cache is something you can do with no fear (except performance): it's only a cache, everything can be (and should be) obtained again from repositories. For those coming from maven 2 land, this may sounds strange, but remember that in Ivy, the cache is not a local repository, things are kept cleanly isolated.

step 3: run the project

Go to chainedresolvers-project directory. And simply run ant.
I:\chained-resolvers\chainedresolvers-project>ant
Buildfile: src\example\chained-resolvers\chainedresolvers-project\build.xml resolve: [ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 :: http://ant.apache.org/ivy/ :: [ivy:retrieve] :: loading settings :: file = C:\dev\data\opensource_workspace\ivy\src\example\chained-resolvers\config\ivysettings.xml [ivy:retrieve] :: resolving dependencies :: [ org.apache | chained-resolvers | working@BEN-ScokartG ] [ivy:retrieve] confs: [default] [ivy:retrieve] found [ commons-lang | commons-lang | 2.0 ] in ibiblio [ivy:retrieve] found [ org.apache | test | 1.0 ] in libraries [ivy:retrieve] downloading http://www.ibiblio.org/maven/commons-lang/jars/commons-lang-2.0.jar ... [ivy:retrieve] ............................................................. [ivy:retrieve] ......................................................... (165kB) [ivy:retrieve] .. (0kB) [ivy:retrieve] [SUCCESSFUL ] [ commons-lang | commons-lang | 2.0 ]/commons-lang.jar[jar] (5928ms) [ivy:retrieve] downloading C:\dev\data\opensource_workspace\ivy\src\example\chained-resolvers\config\repository\test-1.0.jar ... [ivy:retrieve] .. (1kB) [ivy:retrieve] [SUCCESSFUL ] [ org.apache | test | 1.0 ]/test.jar[jar] (10ms) [ivy:retrieve] :: resolution report :: --------------------------------------------------------------------- | | modules || artifacts | | conf | number| search|dwnlded|evicted|| number|dwnlded| --------------------------------------------------------------------- | default | 2 | 2 | 0 | 0 || 2 | 2 | --------------------------------------------------------------------- [ivy:retrieve] :: retrieving :: [ org.apache | chained-resolvers ] [ivy:retrieve] confs: [default] [ivy:retrieve] 2 artifacts copied, 0 already retrieved run: [mkdir] Created dir: C:\dev\data\opensource_workspace\ivy\src\example\chained-resolvers\chainedresolvers-project\build [javac] Compiling 1 source file to C:\dev\data\opensource_workspace\ivy\src\example\chained-resolvers\chainedresolvers-project\build [java] standard message :example world ! [java] capitalized by org.apache.commons.lang.WordUtils : Example World ! [java] upperCased by test.StringUtils : EXAMPLE WORLD ! BUILD SUCCESSFUL Total time: 12 seconds

We can see in the log of the resolve task, that the two dependencies have been retrieved (2 artifacts) and copied to the ivy cache directory (2 downloaded).

The run target succeed in using both commons-lang.jar comming from ibiblio repository and test.jar coming from the local repository.

Going further

This very simple example helps to see how to make a basic setting of two resolvers in a chain. The chain resolver's reference documentation is available for those who would like to know all the features offered by this resolver.

The most interesting things to know that you can try out from this basic example are:

  • a chain is not limited to two nested resolvers, you can use the number you want
  • by setting returnFirst="true", you can have a chain which stops as soon as it has found a result for a given module
  • by setting dual="true", the full chain will be used both for module descriptors and artifacts, while with dual="false", the resolver in the chain which found the module descriptor (if any) is also used for artifacts

go http://ant.apache.org/ivy/history/2.1.0-rc2/tutorial/multiple.html

### 关于 unplugin-auto-import 的 resolvers 配置 `unplugin-auto-import` 是一个用于自动导入 API 或工具函数的插件,广泛应用于 Vue 和 React 等前端框架项目中。通过 `resolvers`,可以自定义需要自动导入的内容及其路径。 以下是关于如何使用或配置 `resolvers` 的详细介绍: #### 自动导入的基础概念 `resolvers` 提供了一种机制来指定哪些模块中的成员应该被自动导入。这些成员通常是一些常用的库函数、组件或者全局变量。例如,在 Vue 项目中,可以通过 `resolvers` 来自动导入 Vuex 中的状态管理方法或 Composition API 函数[^1]。 #### 安装依赖 在开始之前,请确保已经安装了 `unplugin-auto-import` 及其相关依赖项: ```bash npm install unplugin-auto-import --save-dev ``` #### 基本配置示例 以下是一个典型的 Vite/Vue 项目的配置文件 (`vite.config.js`) 示例,展示了如何设置 `resolvers`: ```javascript import AutoImport from 'unplugin-auto-import/vite'; import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'; export default { plugins: [ AutoImport({ imports: ['vue', 'vue-router'], // 默认支持的库 resolvers: [ElementPlusResolver()], // 自定义 resolver dts: './auto-imports.d.ts' // 自动生成声明文件的位置 }) ] }; ``` 在此配置中,`ElementPlusResolver()` 被用来解析并自动导入来自 Element Plus 组件库的相关功能[^2]。 #### 创建自定义 Resolver 如果默认提供的 Resolvers 不满足需求,则可以创建自己的 Resolver。下面是如何实现的一个简单例子: ```javascript function CustomResolver() { return { type: 'component', resolve(name) { if (name.startsWith('Custom')) { return `${name}/path/to/components/${name}.js`; } }, }; } ``` 此代码片段展示了一个名为 `CustomResolver` 的函数,它会尝试匹配以字符串 `"Custom"` 开头的所有名称,并返回对应的模块路径[^3]。 #### 结合 TypeScript 使用 当与 TypeScript 一起工作时,推荐启用 `dts` 参数以便生成类型声明文件。这有助于 IDE 更好地识别和提示已导入的对象。 ```javascript AutoImport({ ... dts: true, }); ``` 以上步骤完成后,重新启动开发服务器即可生效新的 auto-import 设置[^4]。 ### 总结 通过合理利用 `unplugin-auto-import` 插件以及它的核心特性——`resolvers`,开发者能够极大地简化日常编码流程,减少手动引入重复代码的工作量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值