Maven:dependency中scope与type的用法

 scope及其描述:

scope描述
compileThis is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
providedThis is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtimeThis scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
testThis scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.

system

(deprecated)

This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
importThis scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

总结一下:

 compileprovidedruntimetestimport
compilation classpathxx未定
test classpath未定
runtime classpathxx未定

 

type及其描述:

type的默认值为jar,也可以设置为war、bar、pom等。当type=pom时,必须scope=import。

 

参考文档

Introduction to the Dependency Mechanism

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是创建 Maven 项目并使用 Spring Boot 完成食堂菜品投票功能的步骤: 1. 使用 Maven 创建项目 首先,使用 Maven 创建一个新项目。在 Eclipse ,可以通过以下步骤进行操作: - 点击菜单栏的 "File" -> "New" -> "Other" - 在弹出的 "New" 窗口选择 "Maven" -> "Maven Project" - 在 "New Maven Project" 窗口,选择 "Create a simple project",然后点击 "Next" - 在 "New Maven Project" 窗口,填写 "Group Id"、"Artifact Id" 和 "Version",然后点击 "Finish" 2. 导入 Spring Boot 依赖 在 Maven 项目使用 Spring Boot,需要在 `pom.xml` 文件添加 Spring Boot 依赖。以下是一个示例的 `pom.xml` 文件: ``` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>food-vote</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <spring-boot.version>2.3.2.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> </plugin> </plugins> </build> </project> ``` 在这个 `pom.xml` 文件,我们添加了 `spring-boot-starter-web` 和 `spring-boot-starter-data-jpa` 依赖,分别用于构建 Web 应用程序和访问数据库。同时,我们也添加了 `h2` 依赖,用于在开发期间使用嵌入式数据库。 3. 创建实体类和数据库访问接口 我们需要定义一个实体类来表示菜品投票,同时也需要一个数据库访问接口来实现数据的读写。以下是一个示例的实体类和数据库访问接口: ``` @Entity @Table(name = "votes") public class Vote { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private int count; // 省略 getter 和 setter 方法 } @Repository public interface VoteRepository extends JpaRepository<Vote, Long> { } ``` 在这个实体类,我们定义了一个 `Vote` 类,用于表示菜品投票。同时,我们使用了 `@Entity` 和 `@Table` 注解来将该类映射到数据库的 `votes` 表。在 `VoteRepository` 接口,我们使用了 Spring Data JPA 提供的 `JpaRepository` 接口,并将 `Vote` 类和 `Long` 类型的 ID 作为类型参数。这样,我们就可以通过该接口来访问数据库的数据了。 4. 创建控制器和视图 最后,我们需要创建一个控制器来处理用户的请求,并将处理结果返回给用户。同时,我们也需要创建一个视图来展示菜品投票的情况。以下是一个示例的控制器和视图: ``` @Controller public class VoteController { @Autowired private VoteRepository voteRepository; @GetMapping("/") public String index(Model model) { List<Vote> votes = voteRepository.findAll(); model.addAttribute("votes", votes); return "index"; } @PostMapping("/") public String vote(@RequestParam String name) { Vote vote = voteRepository.findByName(name); if (vote == null) { vote = new Vote(); vote.setName(name); vote.setCount(1); } else { vote.setCount(vote.getCount() + 1); } voteRepository.save(vote); return "redirect:/"; } } <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Food Vote</title> </head> <body> <h1>Food Vote</h1> <form method="post"> <input type="text" name="name" placeholder="Enter food name"> <button type="submit">Vote</button> </form> <table> <thead> <tr> <th>Name</th> <th>Count</th> </tr> </thead> <tbody> <tr th:each="vote : ${votes}"> <td th:text="${vote.name}"></td> <td th:text="${vote.count}"></td> </tr> </tbody> </table> </body> </html> ``` 在这个控制器,我们使用 `@GetMapping` 和 `@PostMapping` 注解来分别处理 GET 和 POST 请求。在 `index()` 方法,我们查询数据库的所有菜品投票,并将结果传递给视图。在 `vote()` 方法,我们根据用户输入的菜品名称来更新数据库的菜品投票情况。最后,我们将视图的 HTML 代码放在了 `index.html` 文件,用于展示菜品投票的情况。 这样,我们就完成了使用 Maven 创建项目并使用 Spring Boot 完成食堂菜品投票功能的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值