Taught you how to convert the idea into a project-based (reprint) based eclipse project

1 Consolidation Project

First, copy and delete the item version of the information. This operating system can use the search function. Because the original project is the construction of SVN, so here searching .svn.

  

                                 Search .svn     

To delete all the files in the .svn path, so the project is to become a pure local project slightly. This avoids changes in the original project, then later upload this new project to version control system.

2 idea introduced

Open idea, File -> New -> Project from Existing Sources, open the Import Project dialog box, select the item you want to import, next choice Eclispe:

                               

                                                Import mode selection Eclispe      

Follow-up to the default configuration, click the Next button all the way to completion.

After importing a warning message will pop up in:

The first warning was the discovery of unknown eclipse introduced package. Followed by detection of the spring profile, and finally to the frame may be used to detect.

3 configuration items in the idea of

Click File -> Project Structure

[1] remove the invalid dependent
module -> Dependencies remove invalid dependent.

[2] mark the corresponding function folder

If the project is not standardized document structure, you may need to manually configure the source code [], [], and other resource file path:

[3] Configuration dependencies

Click Libraries -> + number, add dependencies, the general non-Maven project dependencies in the WEB-INF lib folder. If a Web project, you may also need servlet-api (found under the lib tomcat package).

idea can also detect frame dependencies may be used, and put them in the right column.

[4] configuration Facets

Facets -> + number - after> Web, select the current project, and click OK. Then configure on the Web

web.xml file path and web folder path:

 Click the lower right corner of the warning box Create Artifact, create a project development kits:

 在 Facets 中配置 struts2 与 Spring 框架,如果有的话:

        

                 配置 struts2 与 Spring 框架      

【5】配置 tomcat

 点击 + 号 -> Tomcat Server -> Local:

      

                   配置本地 Tomcat             

                          为 tomcat 取个名字      

在 Depolyment 中配置刚才创建的 artifact 包,其中的 Application context 是 web 项目的访问路径前缀:

 

 点击 tomcat 运行后,发现报错啦:

 

这是因为 eclipse 会在代码类的头部加入一个 BOM 头字符,而 idea 是无法解析这个字符的!试过在 java Compiler 中把编译项改为 eclipse 模式,但编译时还是会报类似的错误!

下载一个 批量去除 BOM 头的工具,把 java 代码的 BOM 头都去除:

这样处理后,一般情况下就能通过编译啦O(∩_∩)O~

4 插曲

4.1 spring2.x 的问题

因为是很老的项目,所以用的 spring 版本是 2.5,这在一般情况下没有问题,但如果你用的是 jdk1.8 及以上版本时,就会抛出这样一个错误:

Caused by: java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
    at org.springframework.context.config.ContextNamespaceHandler$1.parse(ContextNamespaceHandler.java:65)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:69)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1297)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
    ... 75 more

明明 jdk1.8 比 1.5 高,为什么会这样呢?原来 spring2.5 启动时会检测 jdk 的版本,而版本号是写死在代码里的!无语咯……

我们可以在项目中新建一个 src 文件夹(记得把它标记为源代码文件夹哦),然后把 JdkVersion.java 放在 org.springframework.core 路径下:

JdkVersion.java

新的 JdkVersion.java 代码如下:

package org.springframework.core;

/**
 * @author Deniro Li ([email protected])
 *         2017/12/5
 */
public class JdkVersion {

    /**
     * Constant identifying the 1.3.x JVM (JDK 1.3).
     */
    public static final int JAVA_13 = 0;

    /**
     * Constant identifying the 1.4.x JVM (J2SE 1.4).
     */
    public static final int JAVA_14 = 1;

    /**
     * Constant identifying the 1.5 JVM (Java 5).
     */
    public static final int JAVA_15 = 2;

    /**
     * Constant identifying the 1.6 JVM (Java 6).
     */
    public static final int JAVA_16 = 3;

    /**
     * Constant identifying the 1.7 JVM (Java 7).
     */
    public static final int JAVA_17 = 4;

    /**
     * Constant identifying the 1.8 JVM (Java 8).
     */
    public static final int JAVA_18 = 5;


    private static final String javaVersion;

    private static final int majorJavaVersion;

    static {
        javaVersion = System.getProperty("java.version");
        // version String should look like "1.4.2_10"

        if (javaVersion.indexOf("1.8.") != -1) {
            majorJavaVersion = JAVA_18;
        } else if (javaVersion.indexOf("1.7.") != -1) {
            majorJavaVersion = JAVA_17;
        } else if (javaVersion.indexOf("1.6.") != -1) {
            majorJavaVersion = JAVA_16;
        } else if (javaVersion.indexOf("1.5.") != -1) {
            majorJavaVersion = JAVA_15;
        } else {
            // else leave 1.4 as default (it's either 1.4 or unknown)
            majorJavaVersion = JAVA_14;
        }
    }


    /**
     * Return the full Java version string, as returned by
     * <code>System.getProperty("java.version")</code>.
     *
     * @return the full Java version string
     * @see System#getProperty(String)
     */
    public static String getJavaVersion() {
        return javaVersion;
    }

    /**
     * Get the major version code. This means we can do things like
     * <code>if (getMajorJavaVersion() < JAVA_14)</code>.
     *
     * @return a code comparable to the JAVA_XX codes in this class
     * @see #JAVA_13
     * @see #JAVA_14
     * @see #JAVA_15
     * @see #JAVA_16
     * @see #JAVA_17
     */
    public static int getMajorJavaVersion() {
        return majorJavaVersion;
    }

    /**
     * Convenience method to determine if the current JVM is at least Java 1.4.
     *
     * @return <code>true</code> if the current JVM is at least Java 1.4
     * @see #getMajorJavaVersion()
     * @see #JAVA_14
     * @see #JAVA_15
     * @see #JAVA_16
     * @see #JAVA_17
     */
    public static boolean isAtLeastJava14() {
        return true;
    }

    /**
     * Convenience method to determine if the current JVM is at least
     * Java 1.5 (Java 5).
     *
     * @return <code>true</code> if the current JVM is at least Java 1.5
     * @see #getMajorJavaVersion()
     * @see #JAVA_15
     * @see #JAVA_16
     * @see #JAVA_17
     */
    public static boolean isAtLeastJava15() {
        return getMajorJavaVersion() >= JAVA_15;
    }

    /**
     * Convenience method to determine if the current JVM is at least
     * Java 1.6 (Java 6).
     *
     * @return <code>true</code> if the current JVM is at least Java 1.6
     * @see #getMajorJavaVersion()
     * @see #JAVA_16
     * @see #JAVA_17
     */
    public static boolean isAtLeastJava16() {
        return getMajorJavaVersion() >= JAVA_16;
    }

}

至此,spring2.5 与 jdk8 的兼容性问题成功解决。

4.2 hibernate 的问题

运行时,抛 hibernate 错误:

Caused by: java.io.FileNotFoundException: class path resource [hibernate] cannot be resolved to URL because it does not exist
    at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:163)
    at org.springframework.core.io.ClassPathResource.getFile(ClassPathResource.java:175)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:660)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    ... 76 more

指的是 hibernate 找不到资源路径。因为 这个项目的 hibernate 资源文件夹下没有文件,而 idea 在开发部署时是不会创建没有包含文件的文件夹的,所以就抛错咯。

手工新建一个无用的 txt 文件即可解决这个问题。

原文链接:https://www.jianshu.com/p/58db34ba46b6

Guess you like

Origin www.cnblogs.com/name-lizonglin/p/12205749.html