使用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测试接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值