zoukankan      html  css  js  c++  java
  • 使用 IDEA + Maven + Git 快速开发 Java Web 应用

    转自:http://my.oschina.net/huangyong/blog/175363

    目录[-]

    0. 引言

    今天想跟大家分享一下我主要的 Java 开发工具,我一般是这样工作的:用 IDEA 写代码,用 Maven 管理 jar 包依赖与项目打包,用 Git 进行代码版本控制。

    关于这三款工具的安装与配置的过程,本文不作说明,有不太明白的朋友,可以给我留言,我会尽力解答。

    下面以开发 Smart 应用为例,描述一下这三款工具的具体使用方法。

    1. 使用 Git 下载源码

    首先,在你的磁盘里找个单独的文件夹来存放 Smart 的所有项目源码,例如:D:Projectsmart,以下称为“工作目录”。

    然后,分别通过 git clone 命令下载 Smart Framework 与 Smart Sample 的源码到本地磁盘上。下载地址如下:

    1. Smart Framwork:http://git.oschina.net/huangyong/smart-framework
    2. Smart Sample:http://git.oschina.net/huangyong/smart-sample
    3. Smart Cache:http://git.oschina.net/huangyong/smart-plugin-cache

    例如:使用 git clone http://git.oschina.net/huangyong/smart-framework 命令,下载 Smart Framework 项目源码。

    执行完以上三条 git clone 命令后,你的工作目录中应该会看到这三个子目录:smart-framework、smart-sample、smart-plugin-cache。

    2. 编写 Maven 项目配置文件

    此时,你可以在工作目录里(也就是在以上三个项目目录的同级)编写一个 Maven 项目配置文件(作为根配置文件),同样需要命名为 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>smart</groupId>
        <artifactId>smart</artifactId>
        <version>1.0</version>
        <packaging>pom</packaging>
    
        <modules>
            <module>smart-framework</module>
            <module>smart-sample</module>
            <module>smart-plugin-cache</module>
        </modules>
    
    </project>
    注:按照原文的方式配置pom会出现很多问题,很多版本号没有写明。修改如下:

    smart-sample下的pom:

    <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.smart</groupId>
    <artifactId>smart</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
    <module>smart-framework</module>
    <module>smart-sample</module>
    <module>smart-plugin-cache</module>
    </modules>

    </project>

    ---------------------------------------------------------------------------------------

    <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">
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
    <groupId>com.smart</groupId>
    <artifactId>smart</artifactId>
    <version>1.0</version>
    <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.smart</groupId>
    <artifactId>smart-sample</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
    <!-- JUnit -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
    <!-- Servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
    </dependency>
    <!-- JSTL -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
    </dependency>
    <!-- MySQL -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.25</version>
    <scope>runtime</scope>
    </dependency>
    <!-- Smart -->
    <dependency>
    <groupId>com.smart</groupId>
    <artifactId>smart-framework</artifactId>
    <version>2.0</version>
    </dependency>
    <dependency>
    <groupId>com.smart</groupId>
    <artifactId>smart-plugin-cache</artifactId>
    <version>1.0</version>
    </dependency>
    <dependency>
    <groupId>com.smart</groupId>
    <artifactId>smart-plugin-ws</artifactId>
    <version>1.1</version>
    </dependency>
    <dependency>
    <groupId>com.smart</groupId>
    <artifactId>smart-plugin-i18n</artifactId>
    <version>1.0</version>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <!-- Compile -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
    <source>1.6</source>
    <target>1.6</target>
    </configuration>
    </plugin>
    <!-- Test -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
    <skipTests>true</skipTests>
    </configuration>
    </plugin>
    <!-- War -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
    <warName>${project.artifactId}</warName>
    </configuration>
    </plugin>
    <!-- Tomcat -->
    <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    </plugin>
    </plugins>
    </build>

    <repositories>
    <!-- OSC Public -->
    <repository>
    <id>osc</id>
    <url>http://maven.oschina.net/content/groups/public/</url>
    </repository>
    <!-- OSC Thirdparty -->
    <repository>
    <id>osc_thirdparty</id>
    <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
    </repository>
    </repositories>

    </project>

    -----------------------------------------------------------------------------------------------------------------

    <?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>
    <groupId>com.smart</groupId>
    <artifactId>smart</artifactId>
    <version>1.0</version>
    <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.smart</groupId>
    <artifactId>smart-plugin-cache</artifactId>
    <version>1.0</version>

    <dependencies>
    <!-- JUnit -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    </dependency>
    <!-- Smart -->
    <dependency>
    <groupId>com.smart</groupId>
    <artifactId>smart-framework</artifactId>
    <version>2.0</version>
    </dependency>
    </dependencies>

    </project>

    ---------------------------------------------------------------------------------------------------------------------

    <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>
    <groupId>com.smart</groupId>
    <artifactId>smart</artifactId>
    <version>1.0</version>
    <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.smart</groupId>
    <artifactId>smart-framework</artifactId>
    <version>2.0</version>

    <dependencies>
    <!-- JUnit -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    </dependency>
    <!-- SLF4J -->
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
    </dependency>
    <!-- Servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    </dependency>
    <!-- Jackson -->
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
    </dependency>
    <!-- CGLib -->
    <dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>

    </dependency>
    <!-- Apache Commons -->
    <dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.5</version>
    </dependency>
    <dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
    </dependency>
    <dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.8.3</version>
    </dependency>
    <dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>

    </dependency>
    <dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.5</version>

    </dependency>
    <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3</version>
    </dependency>
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.4</version>
    </dependency>
    </dependencies>

    </project>

    3. 使用 IDEA 直接打开 Maven 项目

    打开你的 IDEA,选择 Open Project,此时会弹出一个对话框:

    在地址栏中定位到你的工作目录(可以手工编写路径或直接粘贴路径),选择 pom.xml,随后 IDEA 将自动创建一个基于 Maven 项目。

    注意:在 IDEA 的向导界面中一定要选择是 Open Project,而不要选择 Create New Project。也可以使用 Import Project 导入 Maven 项目,但没有直接 Open Project 来得痛快。

    弹指之间,项目已创建完毕!

    4. 编译源码

    先编译一下吧,可以在工具栏中点击这个按钮:

    你也可使用快捷键 Ctrl + F9,显摆一下你的专业。

    随后,IDEA 将编译整个项目源码,一般情况下是没有任何消息的,因为没有消息就是好消息。

    此外,你还可使用 Maven 进行编译。在 IDEA 中点击右侧的 Maven Projects 选项卡,可看到如下界面:

    注意:上图中 smart (root) 表示 Maven 的根配置,也就是你刚才手工编写的那个 pom.xml,由它来管理所有其他的 Maven 项目。

    可双击一下 compile 节点,随后 IDEA 会调用 Maven 进行编译。这样你就不需要在 cmd 里手工输入 Maven 命令了,当然其他的命令你也是可以双击运行。

    5. 在 IDEA 中配置 Tomcat

    注意:建议使用 Tomcat 7.0+

    在 IDEA 中配置 Tomcat,只需进行一下几个步骤:

    首先,点击工具栏中的下拉框,然后点击下拉菜单中的 Edit Configurations 菜单项。

    点击后将弹出一个对话框,你可点击左上角的“+”按钮,新建一个 Local 的 Tomcat Server。

    然后,可对 Tomcat 进行命名,默认是 Unnamed,可修改为 Tomcat。此外,若你不想让 IDEA 自动打开浏览器,去掉 Start browser 复选框即可,我一般都是这么干的。

    随后,部署一个项目,需要切换到上图中的 Deployment 选项卡中,进行简单的配置。

    操作步骤:点击“+”按钮 -> 点击 Artifacts... -> 选择 smart-sample:war exploded -> 修改 Application context 为“/smart-sample”。

    最后,建议你切换回 Server 选项卡,在 On frame deactivation 下拉框下选择 Update resources。

    这样做是为了当你切换出 IDEA 后,可自动更新资源(包括:HTML、CSS、JS、JSP 等),如需要自动编译的话,可使用第三个选项。

    注意:IDEA 12.1.6(最新版)中有一个 Bug,第二项也进行了自动编译,在之前的版本中是不存在的,不知道下个版本会不会解决这个问题,不过此问题不会影响你使用。

    点击 OK 按钮完成 Tomcat 的所有配置,现在可在工具栏中看见 Tomcat 图标了。

    提示:在运行之前,你必须手工创建数据库。比如,Smart Sample 的数据库脚本就在 smart-sample 的 doc 目录下。

    6. 以 Debug 方式运行应用程序

    你只需点一下工具栏中的 Debug 按钮,即可以 Debug 方式运行 Smart Sample 了。

    建议:在开发过程中尽量使用 Debug 方式运行,这样你可以随时在 IDEA 中打断点进行调试。此外,需要说明的是,IDEA 的调试功能非常强大,而且非常好用!

    7. 在 IDEA 中对 Git 进行提交与更新

    在工具栏中也包括了 Git 的常用操作,操作非常方便。

    以上图标分别表示:更新代码、提交代码、查看差异、查看历史、撤销更改。

    此外,你还可以点击最下方的 Changes 选项卡,用来查看本地变更(Local)与提交日志(Log)。

    你可以选择某个版本来查看具体提交的文件内容,还可以同时选择多个版本一起查看,还有很多实用的功能。

    8. 总结

    IDEA 是一款非常优秀的 Java 集成开发环境,用法非常简单,上手也非常快。它对主流技术与工具都有相应的插件支持,你可以自定义插件列表,把对于自己无用的插件禁用掉。

    Maven 是一款非常优秀的项目构建工具,有了它之后,你无需在每个项目中管理一大堆的 jar 包了,此外,它还非常有助于对项目进行打包(打 jar/war 包),当然它的功能绝不仅仅如此。

    Git 是一款非常优秀的代码版本控制系统,它可以有效地实现多人异地办公,此外,你可以提交代码到本地仓库中,然后一次性提交到远程仓库。

    以上三款工具的结合,会大大提高 Java 程序员的战斗能力!在此,强烈推荐大家使用!

  • 相关阅读:
    Java8常用新特性实践
    Presto集群部署
    Exception: Unexpected End Of File(crontab)
    centos6环境下使用yum安装Ambari
    pyspark进行词频统计并返回topN
    七行代码开始flask
    hibernate初步4
    java四大域总结
    servlet中的转发和重定向问题
    一个web页面的访问的过程
  • 原文地址:https://www.cnblogs.com/gaodong/p/3592903.html
Copyright © 2011-2022 走看看