paulwong

ECLIPSE+MAVEN+TOMCAT7 PLUGING+JNDI

开发工具是ECLIPSE,由于是MAVEN WEB项目,不是ECLIPSE项目,测试用的如TOMCAT就不能使用平常的ECLIPSE加TOMCAT的方式了。只能用MAVEN的TOMCAT插件。

MAVEN的这个TOMCAT插件其实就是让MAVEN去启动一个内嵌的TOMCAT服务器,如果项目和这个TOMCAT关联了,项目就可以部署上去。

内嵌的服务器功能扩展,是通过插件的配置进行的。如改端口等,一些复杂的功能如增加数据源,就要配置自定义的server.xml了,但配置了这个后,插件的所有功能都被覆盖了,如context.xml也无效了,代码改变,自动重启TOMCAT等功能也没有了。

数据源只能配在context.xml中,作用范围只在该WEB APP内。

费话不多说,上菜:

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.freelancerj2ee</groupId>
    <artifactId>radar-web</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>radar-web Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <hibernate.version>4.0.1.Final</hibernate.version>
        <mysql.connector.version>5.1.18</mysql.connector.version>
        <slf4j.version>1.6.1</slf4j.version>
        <spring.version>3.1.0.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
    
        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.0.2.RELEASE</version>
        </dependency>
        
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>${mysql.connector.version}</version>
       </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>7.0.42</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-coyote</artifactId>
            <version>7.0.42</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>radar-web</finalName>
        <plugins>
            <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <mode>context</mode>
                    <contextFile>src/main/webapp/META-INF/context.xml</contextFile>
                    <!-- 如果server.xml配了,则其他所有配置都会失效,包括context.xml -->
                    <!-- <serverXml>src/main/tomcatconf/server.xml</serverXml> -->
                    <contextReloadable>true</contextReloadable>
                    <backgroundProcessorDelay>5</backgroundProcessorDelay>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.connector.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.glassfish.maven.plugin</groupId>
                <artifactId>maven-glassfish-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <glassfishDirectory>D:/PAUL/DOWNLOAD/SOFTWARE/DEVELOP/GLASSFISH4/glassfish</glassfishDirectory>
                    <user>admin</user>
                    <passwordFile>pwd</passwordFile>
                    <autoCreate>true</autoCreate>
                    <debug>true</debug>
                    <echo>true</echo>
                    <terse>true</terse>
                    <domain>
                        <name>domain1</name>
                        <adminPort>4848</adminPort>
                        <httpPort>8080</httpPort>
                        <httpsPort>8443</httpsPort>
                        <iiopPort>3700</iiopPort>
                        <jmsPort>7676</jmsPort>
                        <reuse>false</reuse>
                    </domain>
                    <components>
                        <component>
                            <name>${project.artifactId}</name>
                            <artifact>
                                ${project.build.directory}/${project.artifactId}.${project.packaging}
                            </artifact>
                        </component>
                    </components>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>



context.xml,src\main\webapp\META-INF\context.xml。这样的配置,各个竞价如JSP、WEB-INF/classes、lib可以在不同的地方,类路径会取MAVEN的类输出路径,lib会取MAVEN引用的lib。
<Context path="/radar-web"
    docBase
="E:/radar-web/src/main/webapp"
    debug
="0" privileged="true" reloadbale="true">
    
    
    <Resource name="jdbc/myds" 
              auth
="Container"
              type
="javax.sql.DataSource"
              factory
="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              testWhileIdle
="true"
              testOnBorrow
="true"
              testOnReturn
="false"
              validationQuery
="SELECT 1"
              validationInterval
="30000"
              timeBetweenEvictionRunsMillis
="30000"
              maxActive
="100"
              minIdle
="10"
              maxWait
="10000"
              initialSize
="10"
              removeAbandonedTimeout
="60"
              removeAbandoned
="true"
              logAbandoned
="true"
              minEvictableIdleTimeMillis
="30000"
              jmxEnabled
="true"
              jdbcInterceptors
="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
                org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"

              username
="root"  
              password
="12345"
              driverClassName
="com.mysql.jdbc.Driver"
              url
="jdbc:mysql://localhost:3306/html5canvas" 
              
/>

    <Realm className="org.apache.catalina.realm.DataSourceRealm"
           userTable
="userinfo"
           userNameCol
="user_name"
           userCredCol
="password"
           userRoleTable
="userinfo_roles"
           roleNameCol
="role_name"
           localDataSource
="true"
           dataSourceName
="jdbc/myds"/>
           
</Context>


src\main\resources\META-INF\persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="SimplePU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.freelancerj2ee.radar.userinfo.valueobject.UserInfo</class>
        <class>com.freelancerj2ee.radar.userinfo.valueobject.AccountInfo</class>
        <class>com.freelancerj2ee.radar.role.valueobject.Role</class>
        <non-jta-data-source>java:comp/env/jdbc/myds</non-jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>


web.xml加上这一段:
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>java:comp/env/jdbc/myds</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>


这样使用命令
mvn tomcat7:run
即可运行了。


posted on 2013-07-15 13:51 paulwong 阅读(2614) 评论(0)  编辑  收藏 所属分类: J2EEMAVENTOMCAT


只有注册用户登录后才能发表评论。


网站导航: