如何在代码中添加开源开源协议

深入解析开源许可证,重点探讨Apache License 2.0的使用方式和以及如何在源代码中正确添加许可协议。

开源授权内容详解

LICENSE 许可证,使用什么许可证,表示你遵循这个许可证授权方式。

什么是License

许可证混乱就始于你不知道License到底是什么,到底有什么含义。当你对你的产品使用License时,并不意味着你放弃了任何权利,你依然对其拥有原著作权。License只是授予他们于特定权利来使用你的产品。
License只是把你的作品释放到公有领域,或者给各个拷贝赋予权限。也意味着你放弃了版权收入,别人也没有义务把你列为原作者或贡献者。
开放源代码许可协议更容易为他人作出贡献,而不必寻求特别的许可。它也可以保护你作为原创者的权利,至少确认了你的贡献。它还可以保证你的工作不为别人所剽窃。

Apache License 2.0为例许可证作用

  1. 如果你修改了代码,需要在被修改的文件中说明。
  2. 在延伸的代码中(修改和有源代码衍生的代码中)需要带有原来代码中的协议,商标,专利声明和其他原来作者规定需要包含的说明。
  3. 如果再发布的产品中包含一个Notice文件,则在Notice文件中需要带有Apache Licence。你可以在Notice中增加自己的许可,但不可以表现为对Apache Licence构成更改。
  4. Apache Licence也是对商业应用友好的许可。使用者也可以在需要的时候修改代码来满足需要并作为开源或商业产品发布/销售
  5. 你可以修改包名或类名,但还请保留文件中的版权和作者信息。

用一句话概括Apache License就是,你可以用这代码,但是如果开源你必须保留我写的声明;你可以改我的代码,但是如果开源你必须写清楚你改了哪些;你可以加新的协议要求,但不能与我所公布的协议要求产生冲突;你用这代码干什么我不管,但是你不能让我承担任何责任。

开源协议对比

用户需要根据自己需要的开源方式选择使用哪一个开源协议。

开源协议.png

根据授权方式和使用方式特性,添加对应的协议即可。

协议存放位置

通常会在源文件中放置版权声明和法律免责声明,有时甚至在开源项目的每个源文件中都有完整的许可协议,一般也会在每个源文件中放置协议说明。

如何将 Apache 许可证应用于你的作品

在项目根目录放置 LICENSE 文件

官方或 GitHub 上找到协议模板,替换掉特定部分建立项目时候,Github 会自动帮你添加和修改对应的模版,添加上日期和用户内容,以MIT协议为例。
Github新建项目,选择对应的协议MIT

MIT License

Copyright (c) 2025 Robin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

在代码文件头部添加简短声明

MIT License
// Copyright (c) 2025 Your Name
// This source code is licensed under the MIT license found in the LICENSE file.

Apache License 2.0
/*
 * Copyright 2025 Your Name
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

GNU GPL v3
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

BSD 3-Clause License

/*
 * Copyright (c) 2025, Your Name
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * [后面是三条具体条款,可以简略标注 “see LICENSE file”]
 */

LGPL v3

/*
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <https://www.gnu.org/licenses/>.
 */

有道无术,术尚可求也!有术无道,止于术!术法有限,而道法无限!
NextGen Digital... Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...