2.使用idea创建开源项目并上传到oss

来源:互联网 发布:天盾数据恢复 编辑:程序博客网 时间:2024/06/06 02:51

本章讲解如何使用idea创建一个java开源项目,并带大家将此项目上传到oss中

1.新建maven项目


设置GroupId和ArtfactId


修改ProjectName,点击Finish即可创建Maven项目,此项目我是这么架构的,在com.fcibook.quick下,打算创建N个模块,故首先删掉了自动生成的src目录,并将pom中的packaging类型修改为pom


接下来创建maven子模块quick-http,其他的就不多说了,主要是pom配置,先看一下我的pom配置文件

首先是quick-parent的pom文件,此处的pom作为全局配置,内容比较多,比较重要

<?xml version="1.0" encoding="UTF-8"?><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.fcibook.quick</groupId>    <artifactId>quick-parent</artifactId>    <packaging>pom</packaging>    <version>1.2</version>    <name>Quick</name>    <description>quick is smart java project.</description>    <url>http://www.fcibook.com</url>    <modules>        <module>quick-http</module>    </modules>    <dependencies>        <dependency>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <version>3.5.1</version>        </dependency>        <dependency>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-source-plugin</artifactId>            <version>2.2.1</version>        </dependency>        <dependency>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-javadoc-plugin</artifactId>            <version>2.9.1</version>        </dependency>        <dependency>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-gpg-plugin</artifactId>            <version>1.5</version>        </dependency>    </dependencies>    <properties>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <organization>        <name>Fcibook Group</name>        <url>https://github.com/fcibook</url>    </organization>    <licenses>        <license>            <name>The Apache Software License, Version 2.0</name>            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>        </license>    </licenses>    <developers>        <developer>            <name>fcibook</name>            <email>fcibook@126.com</email>        </developer>    </developers>    <scm>        <url>https://github.com/fcibook/quick</url>        <connection>git@github.com/fcibook/quick.git</connection>        <developerConnection>http://www.fcibook.com</developerConnection>    </scm>    <profiles>        <profile>            <id>release</id>            <properties>                <gpg.executable>gpg2</gpg.executable>                <gpg.passphrase>请手动填写passphrase密码</gpg.passphrase>            </properties>            <build>                <plugins>                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-compiler-plugin</artifactId>                        <version>3.5.1</version>                        <configuration>                            <encoding>UTF-8</encoding>                            <source>1.5</source>                            <target>1.5</target>                        </configuration>                    </plugin>                    <!-- Source -->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-source-plugin</artifactId>                        <version>2.2.1</version>                        <executions>                            <execution>                                <phase>package</phase>                                <goals>                                    <goal>jar-no-fork</goal>                                </goals>                            </execution>                        </executions>                        <configuration>                            <attach>true</attach>                        </configuration>                    </plugin>                    <!-- Javadoc -->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-javadoc-plugin</artifactId>                        <version>2.9.1</version>                        <executions>                            <execution>                                <phase>package</phase>                                <goals>                                    <goal>jar</goal>                                </goals>                            </execution>                        </executions>                        <configuration>                            <show>public</show>                            <charset>UTF-8</charset>                            <encoding>UTF-8</encoding>                            <docencoding>UTF-8</docencoding>                        </configuration>                    </plugin>                    <!-- GPG -->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-gpg-plugin</artifactId>                        <version>1.5</version>                        <executions>                            <execution>                                <phase>verify</phase>                                <goals>                                    <goal>sign</goal>                                </goals>                            </execution>                        </executions>                    </plugin>                </plugins>            </build>            <distributionManagement>                <repository>                    <id>private-nexus</id>                    <url>http://localhost:8081/nexus/content/repositories/releases</url>                </repository>                <snapshotRepository>                    <id>private-nexus</id>                    <url>http://localhost:8081/nexus/content/repositories/snapshots</url>                </snapshotRepository>            </distributionManagement>        </profile>    </profiles>    <!--私服配置-->    <repositories>        <repository>            <id>nexus</id>            <url>http://localhost:8081/nexus/content/groups/public/</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>nexus</id>            <url>http://localhost:8081/nexus/content/groups/public/</url>        </pluginRepository>    </pluginRepositories></project>
其次看一下quick-http的pom配置,因为有了全局配置,这里就相对简单很多了
<?xml version="1.0" encoding="UTF-8"?><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">    <parent>        <artifactId>quick-parent</artifactId>        <groupId>com.fcibook.quick</groupId>        <version>1.2</version>    </parent>    <packaging>jar</packaging>    <modelVersion>4.0.0</modelVersion>    <artifactId>quick-http</artifactId>    <name>Quick Http</name>    <description>quick is smart java project.</description>    <url>http://www.fcibook.com</url>    <dependencies>        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>fluent-hc</artifactId>            <version>4.4.1</version>            <scope>provided</scope>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpmime</artifactId>            <version>4.4.1</version>            <scope>provided</scope>            <optional>true</optional>        </dependency>    </dependencies></project>

2.pom配置

接下来我们逐步来解释一下这些pom配置各有什么作用

首先来看一下quick-parent的pom配置,从上往下,调重点看

<version>发布版本号</version><name>项目名称</name><description>项目描述</description><url>项目网址</url><modules>   <module>quick-http</module></modules><dependencies>相关maven插件</dependencies>编码<properties>    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>证书<licenses>    <license>        <name>The Apache Software License, Version 2.0</name>        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>    </license></licenses><developers>    <developer>        <name>开发者</name>        <email>开发者email</email>    </developer></developers><scm>    <url>此处放github的开源项目地址</url>    <connection>此处放github的开源项目clone地址</connection>    <developerConnection>项目官网链接</developerConnection></scm><profiles>    <profile>        <id>release</id>        <properties>            <gpg.executable>gpg2</gpg.executable>            <gpg.passphrase>passphrase密码</gpg.passphrase>        </properties>        <build>            <plugins>                <plugin>                   编译插件相关配置                </plugins>            </build>            <distributionManagement>                <repository>                    <id>发布仓库ID</id>                    <url>发布仓库地址</url>                </repository>                <snapshotRepository>                    <id>快照仓库ID</id>                    <url>快照仓库地址</url>                </snapshotRepository>            </distributionManagement>        </profile>    </profiles></project>

除此之外还有一个仓库的配置,用于拉取第三方java组件库,一般来说我们可以在maven的setting.xml配置文件中配置,pom中无需添加任何配置,如下:

<mirrors>    <mirror>        <id>CN</id>        <name>ZSMY</name>                                                                                                                                 <url>http://localhost:8081/nexus/content/groups/public</url>        <mirrorOf>*</mirrorOf>    </mirror></mirrors> 
在setting.xml即maven全局配置,但如果我们只是单个项目中使用此仓库,就可以直接在pom中配置repositories,此处我们是直接在pom中设置了,所以setting.xml中无需任何配置

接下来,我们在setting.xml配置和仓库ID对应的service,用于配置oss的登录账号密码

<!-- 私服账号 --><server>    <id>private-nexus</id>    <username>admin</username>    <password>admin123</password></server>
注意:此处的ID务必与distributionManagement中的ID相对应

接着我们继续配置gpg,并生成秘钥,相关配置请看另外一篇博客

maven中配置gpg生成秘钥并上传

3.发布部署到OSS

打开Edit Configurations新建Maven配置,并填写clean deploy -P release命令


然后运行即可,或者我们直接进入此项目的根目录,执行mvn命令也可以部署到oss

mvn clean deploy -P release

完成后打开oss,进行搜索即可搜索到相关内容,然后我们就可以在本地环境中使用此项目了