Maven聚合项目的创建

1.项目结构如下

V0Hz8J.png

步骤如下:

V0bE5D.png

V0bS29.md.png

点击Finish

这里父项目需要加入如下的构建依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-releasetrain</artifactId>
                <version>Fowler-SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

再加入springboot的依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>
2.构建子项目

V0bAUO.png

V0bib6.md.png

V0bkVK.md.png

点击next修改名称完成构建 这里我们把a-first当做启动项目,需要在maven中加入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

然后加入启动类:

1
2
3
4
5
6
7
@SpringBootApplication
@ComponentScan("com.test.*")
public class AfirstApplication {
    public static void main(String[] args) {
        SpringApplication.run(AfirstApplication.class);
    }
}

b项目的建立

用同样的方式建立b项目 但不用建立启动类,我们项目a依赖b,如下:

V0bPDx.png

在b中建立包com.test.bsecond,下面建立类TestRead:

1
2
3
4
5
6
7
8
public class TestRead {

    public String eat(){

        return "test eat";
    }
}

在a中建立包com.test.afirst,下面建立包test,再建立类AReadB:

1
2
3
4
5
6
7
8
9
10
11
@Controller
public class AReadB {
    @RequestMapping("/test")
    @ResponseBody
    public  String test() {
        TestRead testRead = new TestRead();
        String eat = testRead.eat();
        return  eat;

    }
}

现在执行mvn install,这个项目下面的所有的都打包完毕

V0bpvR.md.png

然后我们运行启动类的jar包:

V0bCK1.png

访问路径:http://localhost:8080/test 结果如下:

test eat

城南少年与猫 wechat
欢迎您扫一扫订阅我的微信公众号!
0%