尝试使用Gradle - Spring MVC执行bootRun时出错

时间:2015-02-03 16:07:32

标签: spring spring-mvc gradle

我正在尝试使用eclipse使用Gradle-Spring MVC进行bootRun,但出现此错误:

Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' finished with non-zero exit value 1
Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.1-bin.zip'.

在服务器和JPA Hibernate似乎加载后出现。

在控制台中也会显示:

Exception in thread "main" java.lang.IllegalStateException: Tomcat connector in failed state
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:154)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:272)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:132)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:485)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at palmaslab.mapas.config.Application.main(Application.java:71)
:bootRun FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bootRun'.
Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 9.299 secs
[sts] Build failed

这是我的build.gradle文件:

  buildscript {
    ext {
        springBootVersion = '1.1.9.RELEASE'
    }
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
    }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'application'
sourceCompatibility = 1.7
targetCompatibility = 1.7
war {
   version =  '0.1.0'
}
repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
    maven { url "http://maven.springframework.org/milestone" }
    flatDir {
        dirs 'lib'
    }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-tomcat:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-aop:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
    compile("org.springframework.data:spring-data-rest-webmvc")
 compile("org.springframework.boot:spring-boot-starter-security:1.0.2.RELEASE")
    compile("org.springframework.security.oauth:spring-security-oauth2:2.0.0.RC2")
    compile("org.springframework.security.oauth:spring-security-oauth2-javaconfig:1.0.0.M1")
compile("org.hsqldb:hsqldb")
compile("com.h2database:h2")
compile("com.google.guava:guava:17.0")
    compile("org.apache.commons:commons-lang3:3.3.2")
    compile("org.apache.httpcomponents:httpclient:4.3.4")
    compile("com.squareup.retrofit:retrofit:1.6.0")
    compile("commons-io:commons-io:2.4")
compile("com.github.davidmarquis:fluent-interface-proxy:1.3.0")
    compile ("org.springframework.boot:spring-boot-starter-security:1.0.2.RELEASE")
   compile ("javax.servlet:jstl:1.2")
   compile ("commons-fileupload:commons-fileupload:1.3.1")
    testCompile("junit:junit")
    compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect") 
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")
     compile("org.thymeleaf:thymeleaf-spring4")
def poiVersion = "3.10.1"
    compile "org.apache.poi:poi:${poiVersion}"
    compile "org.apache.poi:poi-ooxml:${poiVersion}"
 compile "org.apache.poi:ooxml-schemas:1.1"
}
jar {
    baseName = 'gs-uploading-files'
    version =  '0.1.0'
}
task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

这是我的Application.java类:

  @EntityScan(basePackages= "palmaslab.mapas.repository")
@EnableJpaRepositories(basePackages= "palmaslab.mapas.repository"/*.PostoSaudeRepository.class*/)
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages="palmaslab.mapas.controller")
@Import({palmaslab.mapas.security.SecurityConfiguration.class})
@EnableWebMvc
@PropertySource("application.properties")
public class Application extends WebMvcConfigurerAdapter{
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/", "classpath:/public/" };
public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
         }
     @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(
                DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
            LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
            lef.setDataSource(dataSource);
            lef.setJpaVendorAdapter(jpaVendorAdapter);
            lef.setPackagesToScan("palmaslab.mapas.controller");
            return lef;
        }
@Bean
        public JpaVendorAdapter jpaVendorAdapter() {
            HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
            hibernateJpaVendorAdapter.setShowSql(true);
            hibernateJpaVendorAdapter.setGenerateDdl(true); //Auto creating scheme when true
            hibernateJpaVendorAdapter.setDatabase(Database.H2);//Database type
            return hibernateJpaVendorAdapter;
        }
@Bean  
public SpringTemplateEngine templateEngine() { 
            SpringTemplateEngine engine = new SpringTemplateEngine();
            Set<IDialect> dialects = new HashSet<IDialect>();
            dialects.add(new SpringSecurityDialect());
            dialects.add(new LayoutDialect());
            engine.setAdditionalDialects(dialects);
            LinkedHashSet<ITemplateResolver> templateResolvers = new LinkedHashSet<ITemplateResolver>(2);
            templateResolvers.add(templateResolverServlet());
            templateResolvers.add(layoutTemplateResolverServlet());
            engine.setTemplateResolvers(templateResolvers);
            return engine;
        }
@Bean 
public ServletContextTemplateResolver layoutTemplateResolverServlet() { 
            ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
            templateResolver.setPrefix("/WEB-INF/layout/");
            templateResolver.setSuffix("");
            templateResolver.setTemplateMode("LEGACYHTML5");
            templateResolver.setOrder(1);
            templateResolver.setCacheable(false);
            return templateResolver;
        }
@Bean 
        public ServletContextTemplateResolver templateResolverServlet() { 
            ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
            templateResolver.setPrefix("/WEB-INF/view/");
            templateResolver.setSuffix(".html");
            templateResolver.setTemplateMode("LEGACYHTML5");
        //    templateResolver.setTemplateMode("HTML5");
            templateResolver.setOrder(2);
            templateResolver.setCacheable(false);
            return templateResolver;
        }
@Bean 
        public ViewResolver thymeleafViewResolver() { 
            ThymeleafViewResolver resolver = new ThymeleafViewResolver();
            resolver.setTemplateEngine(templateEngine());
            resolver.setOrder(1);
            resolver.setCharacterEncoding("ISO-8859-1");
            resolver.setCache(false);
            return resolver;
        }
@Bean
    public ServletRegistrationBean dispatcherRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
        registration.addUrlMappings("/");
        registration.setLoadOnStartup(1);
        return registration;
}
@Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
@Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("999999KB");
        factory.setMaxRequestSize("999999KB");
        return factory.createMultipartConfig();
    }
     @Bean
      public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(1000000000);
        return resolver;
      }
@Bean
    public CommonsMultipartResolver filterMultipartResolver() {
        CommonsMultipartResolver resolver=new CommonsMultipartResolver();
        resolver.setDefaultEncoding("ISO-8859-1");
        resolver.setMaxUploadSize(500000000);
        resolver.setMaxInMemorySize(500000000);
        return resolver;
    }
  @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort(8080);
        factory.setSessionTimeout(5, TimeUnit.MINUTES);
        //factory.addErrorPages(new ErrorPage(HttpStatus.404, "/notfound.html"));
        return factory;
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/locals/**")) {
            registry.addResourceHandler("/locals/**").addResourceLocations(
                     "classpath:/locals");
}
    }
}

5 个答案:

答案 0 :(得分:3)

运行Spring Boot应用程序时出现类似的错误消息。在应用程序文件夹里面我运行了./gradlew setMemDb bootRun。

我的解决方案就是跑,

  • ./ gradlew clean
  • ./ gradlew build

然后是原始命令,。/ gradlew setMemDb bootRun。

我猜这个问题的原因可能就是造成这些错误的临时文件。

希望这有帮助

答案 1 :(得分:2)

我遇到了类似的错误,但原因还有别的。所以我认为值得分享:

我的问题是我已经将Tomcat作为单独的服务运行,所以我不得不首先终止它。

仅供参考,命令为:sudo service tomcat8 stop 然后我做了./gradlew clean然后./gradlew bootRun

答案 2 :(得分:1)

每当我们使用bootrun运行项目时,它会自动清理并重建,说你可以使用推荐来运行

./gradlew setMemDb bootRun

希望它在跑步时不会停止。

答案 3 :(得分:1)

我曾经遇到过同样的问题,然后发现个人资料丢失

下面是使用配置文件运行应用程序的命令,这里我们使用dev作为配置文件

  

SPRING_PROFILES_ACTIVE = dev ./gradlew bootRun

答案 4 :(得分:0)

我必须在项目的根路径中执行

gradle clean

并且

gradle build

注意:如果出于任何原因在build命令中无效,请删除路径node_modules并重试

同时将gradle版本更新为最新版本,可以帮助我们