将公共的代码抽离出来,打成公共的lib包的方法

本文详细介绍了如何编辑Maven的pom.xml文件,配置spring-boot-maven-plugin和maven-jar-plugin,以排除特定文件并打包应用。打包完成后,将jar文件上传到Maven私服,以便于其他项目通过依赖管理进行引用。

第一步:编辑pom.xml的

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<executions>
					<execution>
						<id>lib</id>
						<phase>package</phase>
						<goals>
							<goal>jar</goal>
						</goals>
						<configuration>
							<classifier>lib</classifier>
							<excludes>
								<exclude>application.properties</exclude>
								<exclude>com/geexek/htabletemplate/HtabletemplateApplication.class</exclude>
							</excludes>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

注意:这里要排除application.properties和com/geexek/htabletemplate/HtabletemplateApplication.class 打包。

打包后的文件:

在这里插入图片描述

将这个文件上传到maven私服上即可。

获取对应的maven依赖,就可加入到新的项目中

### 如何在 UniApp 中创建和使用公共方法 #### 创建公共方法库文件 为了提高代码的可维护性和重用性,在 UniApp 项目中可以将常用的函数封装成公共方法。通常这些公共方法会被放置在一个单独的文件夹内,比如 `common` 或者 `utils` 文件夹下。 ```javascript // utils/common.js export function formatDate(date, fmt) { let ret; const opt = { "y+": date.getFullYear().toString(), // 年 "M+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "m+": date.getMinutes().toString(), // 分 "s+": date.getSeconds().toString() // 秒 // 存在一毫秒的情况 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0'))) } } return fmt; } ``` 此部分展示了如何定义一个用于日期格式化的公共工具函数[^1]。 #### 导入并调用公共方法 当需要在整个应用程序的不同页面或组件之间共享逻辑时,可以通过 ES6 的 import/export 来引入已编写的公共方法。 ```vue <template> <view>{{ formattedDate }}</view> </template> <script> import { formatDate } from '@/utils/common'; export default { data() { return { currentDate: new Date(), formattedDate: '' }; }, onLoad() { this.formattedDate = formatDate(this.currentDate, 'yyyy-MM-dd HH:mm:ss'); } }; </script> ``` 这段代码片段显示了怎样通过 Vue 组件来加载外部定义的方法,并将其应用于视图层的数据绑定上。 #### 使用 Vuex 进行状态管理(如果适用) 对于更复杂的应用场景,尤其是涉及到多个地方都需要访问相同的状态变量或是业务逻辑的情况下,推荐采用 Vuex 库来进行全局状态管理和分发操作。这不仅有助于保持应用的一致性和简化跨组件通信,而且也方便后期扩展功能和服务端渲染的支持。 ```javascript // store/index.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: {}, mutations: {}, actions: {} }); export default store; // main.js 需要注册该 Store 实例 import store from './store' new Vue({ ..., store }).$mount('#app') ``` 这里介绍了设置 Vuex 商店的基础结构,以便于后续添加更多关于集中存储处理的功能[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值