使用gitea发布软件包

1、新建hello工程

(1)HelloApplication.java

复制代码
package cn.ac.trimps.sv;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(HelloApplication.class, args);
	}
	
	@Override
	public void run(String... arg0) throws Exception {
		
	}
	
}

(2)EnableHello.java

复制代码
package cn.ac.trimps.sv.config;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({HelloConfig.class})
public @interface EnableHello {
 
}

(3)HelloConfig.java

复制代码
package cn.ac.trimps.sv.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("cn.ac.trimps.sv.**")
public class HelloConfig {

}

(4)HelloController.java

复制代码
package cn.ac.trimps.sv.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.ac.trimps.sv.service.HelloService;

@RestController
@RequestMapping("hello")
public class HelloController {

	@Autowired
	HelloService helloService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String helloWorld() {
		return helloService.helloWorld();
	}
	
}

(5)HelloService.java

复制代码
package cn.ac.trimps.sv.service;

import org.springframework.stereotype.Service;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class HelloService {

	public String helloWorld() {
		String str = StrUtil.format("{} {}.", "hello", "world");
		log.info("{}", str);
		return str;
	}
	
}

(6)application.properties

复制代码
logging.config=classpath:logback-plus.xml

server.port=1400

(7)logback-plus.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern>
			<charset>UTF-8</charset>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>INFO</level>
		</filter>
	</appender>
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>/home/logs/hello/stdout.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- daily rollover 保存历史记录到这个文件夹一日起为后缀 -->
			<fileNamePattern>/home/logs/hello/stdout.log.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<!-- keep 30 days' worth of history -->
			<maxHistory>30</maxHistory>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern>
			<charset>UTF-8</charset> <!-- 此处设置字符集 -->
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>/home/logs/hello/stderr.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- daily rollover 保存历史记录到这个文件夹一日起为后缀 -->
			<fileNamePattern>/home/logs/hello/stderr.log.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<!-- keep 30 days' worth of history -->
			<maxHistory>30</maxHistory>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern>
			<charset>UTF-8</charset> <!-- 此处设置字符集 -->
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<root level="INFO">
		<appender-ref ref="CONSOLE"></appender-ref>
		<appender-ref ref="FILE"></appender-ref>
		<appender-ref ref="FILE_ERROR"></appender-ref>
	</root>
</configuration>

(8)build.gradle

复制代码
plugins {
	id 'org.springframework.boot' version '2.7.18'
	id 'io.spring.dependency-management' version '1.0.15.RELEASE'
	id 'java'
	id 'eclipse'
	id 'maven-publish'
}

group = 'cn.ac.trimps.sv'
version = '0.0.1'
sourceCompatibility = 1.8

repositories {
	maven {
		url 'https://maven.aliyun.com/repository/public'
    }
	mavenCentral()
}

jar {
	// 移除分类器(默认是 'plain')
	archiveClassifier = ''
}

bootJar {
	// 自定义文件名称
	archiveFileName = "${project.name}-${project.version}.jar"
    launchScript()
}

publishing {
	publications {
        mavenJava(MavenPublication) {
            // 发布普通Jar
            from components.java
        }
    }
    repositories {
        maven {
            name = 'Gitea'
            url = uri('http://127.0.0.1:3004/api/packages/huangwending/maven')
            credentials(HttpHeaderCredentials) {
                // 令牌名称
                name = 'Authorization'
                // 令牌
                value = 'token 033b2cfce7541387c1e272be45275cd926dc922c'
            }
            authentication {
                header(HttpHeaderAuthentication)
            }
        }
    }
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'cn.hutool:hutool-all:5.8.37'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

2、发布hello工程jar包至本地gitea仓库

命令:gradle clean publishAllPublicationsToGiteaRepository --info

复制代码
PS G:\workspace\study\hello> gradle clean publishAllPublicationsToGiteaRepository --info
Initialized native services in: E:\.gradle-6.8.3\native
The client will now receive all logging from the daemon (pid: 22512). The daemon log file: E:\.gradle-6.8.3\daemon\6.8.3\daemon-22512.out.log
Starting 30th build in daemon [uptime: 3 hrs 5 mins 0.129 secs, performance: 99%, GC rate: 0.00/s, heap usage: 8% of 341.5 MiB, non-heap usage: 31% of 256 MiB]
Using 8 worker leases.
Watching the file system is disabled
Starting Build
Settings evaluated using settings file 'G:\workspace\study\hello\settings.gradle'.
Projects loaded. Root project using build file 'G:\workspace\study\hello\build.gradle'.
Included projects: [root project 'hello']

> Configure project :
Evaluating root project 'hello' using build file 'G:\workspace\study\hello\build.gradle'.
Compiling build file 'G:\workspace\study\hello\build.gradle' using SubsetScriptTransformer.
Applying dependency management to configuration 'bootArchives' in project 'hello'
Applying dependency management to configuration 'archives' in project 'hello'
Applying dependency management to configuration 'default' in project 'hello'
Applying dependency management to configuration 'compile' in project 'hello'
Applying dependency management to configuration 'implementation' in project 'hello'
Applying dependency management to configuration 'runtime' in project 'hello'
Applying dependency management to configuration 'compileOnly' in project 'hello'
Applying dependency management to configuration 'compileClasspath' in project 'hello'
Applying dependency management to configuration 'annotationProcessor' in project 'hello'
Applying dependency management to configuration 'runtimeOnly' in project 'hello'
Applying dependency management to configuration 'runtimeClasspath' in project 'hello'
Applying dependency management to configuration 'testCompile' in project 'hello'
Applying dependency management to configuration 'testImplementation' in project 'hello'
Applying dependency management to configuration 'testRuntime' in project 'hello'
Applying dependency management to configuration 'testCompileOnly' in project 'hello'
Applying dependency management to configuration 'testCompileClasspath' in project 'hello'
Applying dependency management to configuration 'testAnnotationProcessor' in project 'hello'
Applying dependency management to configuration 'testRuntimeOnly' in project 'hello'
Applying dependency management to configuration 'testRuntimeClasspath' in project 'hello'
Applying dependency management to configuration 'apiElements' in project 'hello'
Applying dependency management to configuration 'runtimeElements' in project 'hello'
Applying dependency management to configuration 'developmentOnly' in project 'hello'
Applying dependency management to configuration 'productionRuntimeClasspath' in project 'hello'
Compiling build file 'G:\workspace\study\hello\build.gradle' using BuildScriptTransformer.
All projects evaluated.
Selected primary task 'clean' from project :
Selected primary task 'publishAllPublicationsToGiteaRepository' from project :
Tasks to be executed: [task ':clean', task ':compileJava', task ':processResources', task ':classes', task ':jar', task ':generateMetadataFileForMavenJavaPublication', task ':generatePomFileForMavenJavaPublication', task ':publishMavenJavaPublicationToGiteaRepository', task ':publishAllPublicationsToGiteaRepository']
Tasks that were excluded: []
:clean (Thread[Execution worker for ':',5,main]) started.

> Task :clean UP-TO-DATE
Caching disabled for task ':clean' because:
  Build cache is disabled
Task ':clean' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
:clean (Thread[Execution worker for ':',5,main]) completed. Took 0.022 secs.
:compileJava (Thread[Execution worker for ':',5,main]) started.

> Task :compileJava
Resolving global dependency management for project 'hello'
Excluding []
Excluding []
Caching disabled for task ':compileJava' because:
  Build cache is disabled
Task ':compileJava' is not up-to-date because:
  No history is available.
The input changes require a full rebuild for incremental task ':compileJava'.
Invalidating in-memory cache of G:\workspace\study\hello\.gradle\6.8.3\javaCompile\classAnalysis.bin
Invalidating in-memory cache of G:\workspace\study\hello\.gradle\6.8.3\javaCompile\jarAnalysis.bin
Invalidating in-memory cache of G:\workspace\study\hello\.gradle\6.8.3\javaCompile\taskHistory.bin
Full recompilation is required because no incremental change information is available. This is usually caused by clean builds or changing compiler arguments.
Compiling with JDK Java compiler API.
Created classpath snapshot for incremental compilation in 0.066 secs. 1 duplicate classes found in classpath (see all with --debug).
:compileJava (Thread[Execution worker for ':',5,main]) completed. Took 4.609 secs.
:processResources (Thread[Execution worker for ':',5,main]) started.

> Task :processResources
Caching disabled for task ':processResources' because:
  Build cache is disabled
Task ':processResources' is not up-to-date because:
  No history is available.
:processResources (Thread[Execution worker for ':',5,main]) completed. Took 0.108 secs.
:classes (Thread[Execution worker for ':',5,main]) started.

> Task :classes
Skipping task ':classes' as it has no actions.
:classes (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:jar (Thread[Execution worker for ':',5,main]) started.

> Task :jar
Caching disabled for task ':jar' because:
  Build cache is disabled
Task ':jar' is not up-to-date because:
  No history is available.
:jar (Thread[Execution worker for ':',5,main]) completed. Took 0.127 secs.
:generateMetadataFileForMavenJavaPublication (Thread[Execution worker for ':',5,main]) started.

> Task :generateMetadataFileForMavenJavaPublication
Caching disabled for task ':generateMetadataFileForMavenJavaPublication' because:
  Build cache is disabled
Task ':generateMetadataFileForMavenJavaPublication' is not up-to-date because:
  Task.upToDateWhen is false.
:generateMetadataFileForMavenJavaPublication (Thread[Execution worker for ':',5,main]) completed. Took 0.116 secs.
:generatePomFileForMavenJavaPublication (Thread[Execution worker for ':',5,main]) started.

> Task :generatePomFileForMavenJavaPublication
Caching disabled for task ':generatePomFileForMavenJavaPublication' because:
  Build cache is disabled
Task ':generatePomFileForMavenJavaPublication' is not up-to-date because:
  Task.upToDateWhen is false.
:generatePomFileForMavenJavaPublication (Thread[Execution worker for ':',5,main]) completed. Took 0.358 secs.
:publishMavenJavaPublicationToGiteaRepository (Thread[Execution worker for ':',5,main]) started.

> Task :publishMavenJavaPublicationToGiteaRepository
Caching disabled for task ':publishMavenJavaPublicationToGiteaRepository' because:
  Build cache is disabled
Task ':publishMavenJavaPublicationToGiteaRepository' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Publishing to repository 'Gitea' (http://127.0.0.1:3004/api/packages/huangwending/maven)
Uploading hello-0.0.1.jar to /api/packages/huangwending/maven/cn/ac/trimps/sv/hello/0.0.1/hello-0.0.1.jar
Uploading hello-0.0.1.pom to /api/packages/huangwending/maven/cn/ac/trimps/sv/hello/0.0.1/hello-0.0.1.pom
Uploading hello-0.0.1.module to /api/packages/huangwending/maven/cn/ac/trimps/sv/hello/0.0.1/hello-0.0.1.module
Uploading maven-metadata.xml to /api/packages/huangwending/maven/cn/ac/trimps/sv/hello/maven-metadata.xml
:publishMavenJavaPublicationToGiteaRepository (Thread[Execution worker for ':',5,main]) completed. Took 1.793 secs.
:publishAllPublicationsToGiteaRepository (Thread[Execution worker for ':',5,main]) started.

> Task :publishAllPublicationsToGiteaRepository
Skipping task ':publishAllPublicationsToGiteaRepository' as it has no actions.
:publishAllPublicationsToGiteaRepository (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.

BUILD SUCCESSFUL in 14s
7 actionable tasks: 6 executed, 1 up-to-date
PS G:\workspace\study\hello>

3、新建test工程,并引用发布的依赖

(1)TestApplication.java

复制代码
package cn.ac.trimps.sv;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

import cn.ac.trimps.sv.config.EnableHello;

@EnableHello
@Configuration
@SpringBootApplication
public class TestApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}
	
	@Override
	public void run(String... arg0) throws Exception {
		
	}
	
}

(2)TestController.java

复制代码
package cn.ac.trimps.sv.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.ac.trimps.sv.service.HelloService;

@RestController
@RequestMapping("test")
public class TestController {

	@Autowired
	HelloService helloService;
	
	@RequestMapping(value = "f1", method = RequestMethod.GET)
	public String f1() {
		return helloService.helloWorld();
	}
	
}

(3)application.properties

复制代码
logging.config=classpath:logback-plus.xml

server.port=1401

(4)logback-plus.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern>
			<charset>UTF-8</charset>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>INFO</level>
		</filter>
	</appender>
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>/home/logs/test/stdout.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- daily rollover 保存历史记录到这个文件夹一日起为后缀 -->
			<fileNamePattern>/home/logs/test/stdout.log.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<!-- keep 30 days' worth of history -->
			<maxHistory>30</maxHistory>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern>
			<charset>UTF-8</charset> <!-- 此处设置字符集 -->
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>/home/logs/test/stderr.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- daily rollover 保存历史记录到这个文件夹一日起为后缀 -->
			<fileNamePattern>/home/logs/test/stderr.log.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<!-- keep 30 days' worth of history -->
			<maxHistory>30</maxHistory>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern>
			<charset>UTF-8</charset> <!-- 此处设置字符集 -->
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<root level="INFO">
		<appender-ref ref="CONSOLE"></appender-ref>
		<appender-ref ref="FILE"></appender-ref>
		<appender-ref ref="FILE_ERROR"></appender-ref>
	</root>
</configuration>

(5)build.gradle

复制代码
plugins {
	id 'org.springframework.boot' version '2.7.18'
	id 'io.spring.dependency-management' version '1.0.15.RELEASE'
	id 'java'
	id 'eclipse'
}

group = 'cn.ac.trimps.sv'
version = '0.0.1'
sourceCompatibility = 1.8

repositories {
	maven {
        url 'http://127.0.0.1:3004/api/packages/huangwending/maven'
        allowInsecureProtocol = true
    }
    maven {
        url 'https://maven.aliyun.com/repository/public'
    }
    mavenCentral()
}

bootJar {
    launchScript()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'cn.ac.trimps.sv:hello:0.0.1'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

4、运行test工程,使用postman测试接口

相关推荐
Clownseven16 天前
Gitea Webhook教程:实现git push后自动部署更新网站 (CI/CD入门)
git·ci/cd·gitea
Clownseven25 天前
如何搭建私有Git服务器?Gitea安装与配置超详细教程 (替代GitHub/Gitee)
服务器·git·gitea
马特说1 个月前
macOS 搭建 Gitea 私有 Git 服务器教程
git·macos·gitea
第四维度41 个月前
【Debian】4-‌1 Gitea简介以及与其他git方案差异
git·gitea
第四维度41 个月前
【Debian】4-‌2 Gitea搭建
运维·debian·gitea
猫头虎1 个月前
GitHub下载教程:2025年最新详解从GitHub上传、下载文件、子目录与完整项目【图文教程】
git·svn·gitee·开源·github·gitea·gitcode
问~3 个月前
gitea本地部署代码托管后仓库的新建与使用(配置好ssh密钥后仍然无法正常克隆仓库是什么原因)
运维·ssh·gitea
丁劲犇3 个月前
开始在本地部署自己的 Gitea 服务器
运维·服务器·gitea
*neverGiveUp*3 个月前
本地分支git push 报错 fatal: The current branch XXXX has no upstream branch.
前端·git·gitea