简体   繁体   中英

Why does my pom.xml doesn't include or generate class files in final war built?

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:b="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/> 
    <b:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>

    <security:http auto-config='true' use-expressions='true'>
         <security:form-login login-page="/login" default-target-url="/loginSuccess" 
            authentication-failure-url="/home?authfailed=true" 
            username-parameter="mobileNumber"
            password-parameter="password"
            always-use-default-target="true"/> 
          <security:intercept-url pattern="/loginCheck" access="hasRole('VERIFIED_USER')"/>
          <security:intercept-url pattern="/loginSuccess" access="hasAuthority('ROLE_VERIFIED_USER')"/>
          <security:intercept-url pattern="/home" access="permitAll" />
          <security:intercept-url pattern="/News" access="permitAll" />

          <security:logout invalidate-session="true" logout-url="/logout"/>          
    </security:http>

       <security:authentication-manager erase-credentials="false">
            <security:authentication-provider ref="myAuthenticationProvider">
            </security:authentication-provider> 
       </security:authentication-manager> 


    <b:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

    <b:bean id="myAuthenticationProvider" class="com.ct.www.provider.CustomAuthenticationProvider">
    </b:bean>   

    <security:global-method-security secured-annotations="enabled"/>

    <b:bean id="personService" class="com.ct.www.service.PersonServiceImpl">
        <b:property name="personDAO" ref="personDAO"></b:property>
    </b:bean>

    <b:bean id="personDAO" class="com.ct.www.dao.PersonDAOImpl">
        <b:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </b:bean> 

    <b:bean id="articleService" class="com.ct.www.service.ArticleServiceImpl">
        <b:property name="articleDAO" ref="articleDAO"></b:property>
    </b:bean>

    <b:bean id="articleDAO" class="com.ct.www.dao.ArticleDAOImpl">
        <b:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </b:bean> 
<!-- For hashing and salting user passwords -->
    <b:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>   

<!--     <b:bean id="basicAuthenticationFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
        <b:constructor-arg ref="authenticationManager" />
         <b:constructor-arg ref="authenticationEntryPoint" />

    </b:bean> -->

    <!--  <b:bean id="authenticationEntryPoint" 
        class="com.ct.www.provider.PlainTextBasicAuthenticationEntryPoint">
        <b:property name="realmName" value="http://localhost:8080" />
    </b:bean> 
     -->
  <b:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <b:property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
        <b:property name="url" value="jdbc:mysql://22.66.111.44:3306/ct?autoReconnect=true&amp;characterEncoding=utf8" />
        <b:property name="username" value="root" />
        <b:property name="password" value="pwd" />
  </b:bean>


    <!--  Form Validator -->

    <b:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <b:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </b:bean> 


    <!-- Hibernate 4 SessionFactory Bean definition -->
    <b:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <b:property name="dataSource" ref="dataSource" />
        <b:property name="packagesToScan">
            <b:list>
                <b:value>com.ct.www.model</b:value>
            </b:list>
        </b:property>
        <b:property name="hibernateProperties">
            <b:props>
                <b:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </b:prop>
                <b:prop key="hibernate.show_sql">true</b:prop>
            </b:props>
        </b:property>
    </b:bean>

    <context:component-scan base-package="com.ct.www" /> 

    <annotation-driven />

<!--    Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <!-- <b:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <b:property name="prefix" value="/WEB-INF/views/" />
        <b:property name="suffix" value=".jsp" />
    </b:bean>     -->

</b:beans>

在此处输入图片说明

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <!-- the purpose of context-param element is to define "context initialization parameters" that is available to all servlets in the web app -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
     <param-value>classpath:/spring/appServlet/spring-security.xml</param-value>
  </context-param>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>  
  <session-config>
        <session-timeout>
            2
        </session-timeout>
    </session-config>

  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- Actually the Dispatcher servlet should be appServlet-context.xml under WEB-INF.
    But we can override and specify the alternate location and specialized name, using init-param element tag -->
    <!-- init-param tag is used to initialize servlet initialization parameter specific to a particular servlet. 
    That is why used within an servlet. -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:/spring/appServlet/servlet-context.xml
            </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>  

</web-app>

I have this project set up and it works fine in my local, but when I deploy it to prod , I get following error. Btw, both run at tomcat 8. I Don't think the spring vs. spring-security version is an issue as suggested in other answer. Also, I'm not comfortable with using @Service in one of the service/authentication provider as it works in localhost. Please help.

org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframewo
rk.web.context.ContextLoaderListener
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.De
faultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springf
ramework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor a
rgument with key [5]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenti
cationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframe
work.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.co
nfig.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'or
g.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationEx
ception: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'myAuthenticationProvider' while setting constructor argument with key [0]; 
nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.ct.www.provider.CustomAuthenticationProvider] for bean with name 'myAuthenticationProv
ider' defined in class path resource [spring/appServlet/spring-security.xml]; nested exception is java.lang.ClassNotFoundException: com.ct.www.provider.CustomAuthenticationProvider

The stack trace is really big and nested, but last line explains all.

在此处输入图片说明

This is my pom.xml

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ct.www</groupId>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
        <spring.security.version>4.0.1.RELEASE</spring.security.version>
        <apachetiles.version>3.0.8</apachetiles.version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>4.3.5.Final</hibernate.version>
        <jackson.version>1.9.10</jackson.version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        <!--    <exclusions>
                Exclude Commons Logging in favor of SLF4j
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions> -->
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
            <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
     <!-- Apache Tiles -->
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-core</artifactId>
            <version>${apachetiles.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-api</artifactId>
            <version>${apachetiles.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-servlet</artifactId>
            <version>${apachetiles.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>${apachetiles.version}</version>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Twilio -->
        <dependency>
            <groupId>com.twilio.sdk</groupId>
            <artifactId>twilio-java-sdk</artifactId>
            <version>4.1.0</version>
            <scope>compile</scope>
        </dependency>

        <!--  Spring Security  -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
            <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

        <!-- Spring Security JSP Taglib -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</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>

        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>   

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
       <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

        <!-- MySQL -->
         <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>                 
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>              
                <configuration>                 
                  <webXml>WebContent/WEB-INF/web.xml</webXml>                   
                </configuration>
            </plugin> 
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>
    <artifactId>ct</artifactId>
    <name>ct</name>
</project>

I'm building in the directory where I have pom.xml by using mvn clean install -DskipTests and upload the war to tomcat8 in prod.

I was getting this error in pom.xml

[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ --- [INFO] No sources to compile

and then I updated it to 3.3 and that is gone. But still I'm getting the ClassNotFoundException exception and I don't see java classes in my war when I extract it.

It loooks that com.ct.www.provider.CustomAuthenticationProvider is missing from your production environment. (and you production artifact). Your local server may source if from some other classloader - also double check you build environment and IDE setup

OK. The issue is because my pom.xml doesn't include the class files and other xml files into the final war that's been built.

This resolved my issue. https://stackoverflow.com/a/49123071/3705478

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM