跳到内容

包管理器的原理、Maven 基础、语义化版本都学了。现在动手。


热身(5 分钟,必做)

bash
pip list                  # Python 库
npm list -g --depth=0     # 全局 Node 包
du -sh ~/.m2/repository/  # Maven 仓库

挑战:Maven + Jackson 读 JSON(选做)

bash
mvn archetype:generate -DgroupId=com.example -DartifactId=json-demo \
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

pom.xml 加 Jackson 2.15.2,写代码读 JSON 并 mvn compile

java
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
    public static void main(String[] args) throws Exception {
        ObjectMapper m = new ObjectMapper();
        java.util.Map u = m.readValue(
            "{\"name\":\"Alice\"}", java.util.Map.class);
        System.out.println(u.get("name"));
    }
}
bash
mvn compile
mvn dependency:tree

常见排障

排障 1:pip Permission denied

原因:系统级 Python 无写权限。正解:用虚拟环境,绝不 sudo pip install

bash
python -m venv venv && source venv/bin/activate
pip install requests
deactivate

排障 2:npm install 卡住

原因:默认源在国内慢。正解:换国内镜像。

bash
npm config set registry https://registry.npmmirror.com
npm install
# Python 同理:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

排障 3:Java ClassNotFoundException

原因:jar 包冲突。排查:mvn dependency:tree。用 <exclusions> 排除:

xml
<dependency>
  <groupId>com.example</groupId>
  <artifactId>lib-b</artifactId>
  <version>1.0</version>
  <exclusions>
    <exclusion>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>

排障 4:lock 文件该不该提交? 都提交。node_modules/__pycache__/ 才放 .gitignore


验收:能用 dependency:tree 查依赖、会排 Permission denied、会换镜像、会用 <exclusions>、明白 lock 文件作用。

→ 下一章

代码写了一堆,程序报错了——别慌。下一站:读懂报错、查日志、写最小复现。

下一章预告:调试与排错

Built with VitePress | Software Systems Atlas