code.gitea.io/gitea@v1.22.3/docs/content/usage/packages/maven.zh-cn.md (about) 1 --- 2 date: "2021-07-20T00:00:00+00:00" 3 title: "Maven 软件包注册表" 4 slug: "maven" 5 sidebar_position: 60 6 draft: false 7 toc: false 8 menu: 9 sidebar: 10 parent: "packages" 11 name: "Maven" 12 sidebar_position: 60 13 identifier: "maven" 14 --- 15 16 # Maven 软件包注册表 17 18 为您的用户或组织发布 [Maven](https://maven.apache.org) 软件包。 19 20 ## 要求 21 22 要使用 Maven 软件包注册表,您可以使用 [Maven](https://maven.apache.org/install.html) 或 [Gradle](https://gradle.org/install/)。 23 以下示例使用 `Maven` 和 `Gradle Groovy`。 24 25 ## 配置软件包注册表 26 27 要注册软件包注册表,首先需要将访问令牌添加到 [`settings.xml`](https://maven.apache.org/settings.html) 文件中: 28 29 ```xml 30 <settings> 31 <servers> 32 <server> 33 <id>gitea</id> 34 <configuration> 35 <httpHeaders> 36 <property> 37 <name>Authorization</name> 38 <value>token {access_token}</value> 39 </property> 40 </httpHeaders> 41 </configuration> 42 </server> 43 </servers> 44 </settings> 45 ``` 46 47 然后在项目的 `pom.xml` 文件中添加以下部分: 48 49 ```xml 50 <repositories> 51 <repository> 52 <id>gitea</id> 53 <url>https://gitea.example.com/api/packages/{owner}/maven</url> 54 </repository> 55 </repositories> 56 <distributionManagement> 57 <repository> 58 <id>gitea</id> 59 <url>https://gitea.example.com/api/packages/{owner}/maven</url> 60 </repository> 61 <snapshotRepository> 62 <id>gitea</id> 63 <url>https://gitea.example.com/api/packages/{owner}/maven</url> 64 </snapshotRepository> 65 </distributionManagement> 66 ``` 67 68 | 参数 | 描述 | 69 | -------------- | ------------------------------------------------------------------------------------- | 70 | `access_token` | 您的[个人访问令牌](development/api-usage.md#通过-api-认证) | 71 | `owner` | 软件包的所有者 | 72 73 ### Gradle variant 74 75 如果您计划在项目中添加来自 Gitea 实例的一些软件包,请将其添加到 repositories 部分中: 76 77 ```groovy 78 repositories { 79 // other repositories 80 maven { url "https://gitea.example.com/api/packages/{owner}/maven" } 81 } 82 ``` 83 84 在 Groovy gradle 中,您可以在发布部分中包含以下脚本: 85 86 ```groovy 87 publishing { 88 // 其他发布设置 89 repositories { 90 maven { 91 name = "Gitea" 92 url = uri("https://gitea.example.com/api/packages/{owner}/maven") 93 94 credentials(HttpHeaderCredentials) { 95 name = "Authorization" 96 value = "token {access_token}" 97 } 98 99 authentication { 100 header(HttpHeaderAuthentication) 101 } 102 } 103 } 104 } 105 ``` 106 107 ## 发布软件包 108 109 要发布软件包,只需运行以下命令: 110 111 ```shell 112 mvn deploy 113 ``` 114 115 或者,如果您使用的是 Gradle,请使用 `gradle` 命令和 `publishAllPublicationsToGiteaRepository` 任务: 116 117 ```groovy 118 ./gradlew publishAllPublicationsToGiteaRepository 119 ``` 120 121 如果您想要将预构建的软件包发布到注册表中,可以使用 [`mvn deploy:deploy-file`](https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html) 命令: 122 123 ```shell 124 mvn deploy:deploy-file -Durl=https://gitea.example.com/api/packages/{owner}/maven -DrepositoryId=gitea -Dfile=/path/to/package.jar 125 ``` 126 127 | 参数 | 描述 | 128 | ------- | -------------- | 129 | `owner` | 软件包的所有者 | 130 131 如果存在相同名称和版本的软件包,您无法发布该软件包。您必须先删除现有的软件包。 132 133 ## 安装软件包 134 135 要从软件包注册表中安装 Maven 软件包,请在项目的 `pom.xml` 文件中添加新的依赖项: 136 137 ```xml 138 <dependency> 139 <groupId>com.test.package</groupId> 140 <artifactId>test_project</artifactId> 141 <version>1.0.0</version> 142 </dependency> 143 ``` 144 145 在 `Gradle Groovy` 中类似的操作如下: 146 147 ```groovy 148 implementation "com.test.package:test_project:1.0.0" 149 ``` 150 151 然后运行: 152 153 ```shell 154 mvn install 155 ``` 156 157 ## 支持的命令 158 159 ``` 160 mvn install 161 mvn deploy 162 mvn dependency:get: 163 ```