gpt4 book ai didi

java - Maven:多模块 WAR 构建为库项目提供 "Unable to find main class"

转载 作者:行者123 更新时间:2023-11-28 22:11:25 25 4
gpt4 key购买 nike

我正在开发一个带有 spring boot 的多模块 web 项目,应该作为 war 部署到 tomcat 中。我不想为 CLI 使用嵌入式 tomcat。

该项目包括:

  • de.dpt.app - 应用程序的主要部分。
  • de.dpt.gen - 库:实体和 ORM 映射(生成)
  • de.shopify.api - 库:Shopify REST Api 的实现

没有一个项目应该需要一个主类,仍然在运行“mvn install”时我得到..

org.apache.maven.lifecycle.LifecycleExecutionException: 
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage (default)
on project de.shopify.api: Execution default of goal
org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage failed:
Unable to find main class

我不明白为什么模块“de.shopify.api”不会构建,而“de.dpt.gen”会构建,因为 poms 非常相似。你能帮帮我吗?

parent/pom.xml(父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>de.dpt</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>de.dpt.parent</name>
<modules>
<module>de.dpt.gen</module>
<module>de.dpt.app</module>
<!-- <module>de.dpt.amazon-batch</module> -->
<module>de.shopify.api</module>
</modules>



<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.6</java.version>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<!-- <tomcat.version>7.0.63</tomcat.version> -->
<tomcat.version>7.0.59</tomcat.version>
<servlet-api.version>3.0.0</servlet-api.version>
</properties>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- only 1.1.10 works for tomcat 7 -->
<!-- <version>1.1.10.RELEASE</version> -->
<!-- <version>1.2.5.RELEASE</version> -->
<version>1.3.5.RELEASE</version><!-- to get a newer jackson version -->
</parent>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<outputDirectory>webapps</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>

</project>

de.dpt.gen/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>
<parent>
<groupId>de.dpt</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>de.dpt.gen</artifactId>
<packaging>jar</packaging>


<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>


<dependency><!-- handle conversion to json; supports circular references -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.voodoodyne.jackson.jsog</groupId>
<artifactId>jackson-jsog</artifactId>
<version>1.1</version>
</dependency>

</dependencies>


<version>0.0.1-SNAPSHOT</version>
</project>

de.shopify.api/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>
<parent>
<groupId>de.dpt</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>de.shopify.api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>


<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>



</dependencies>

</project>

de.dpt.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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.dpt</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>de.dpt.app</artifactId>
<packaging>war</packaging>

<properties>
<tomcat.version>7.0.59</tomcat.version>
<org.mapstruct.version>1.1.0.Final</org.mapstruct.version>
</properties>


<build><pluginManagement>
<plugins>



<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin,
like in README.md -->
<version>1.0</version>

<configuration>
<workingDirectory>src/main/resources/web-app</workingDirectory>
</configuration>

<executions>

<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v5.3.0</nodeVersion>
<npmVersion>3.3.12</npmVersion>
</configuration>
</execution>

<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<!-- Optional configuration which provides for running any npm command -->
<configuration>
<arguments>install</arguments>
</configuration>
</execution>


<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<configuration>
<arguments>dist --no-color</arguments>
</configuration>
</execution>



</executions>
</plugin>


<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<configuration>
<defaultOutputDirectory>
${project.build.directory}/generated-sources
</defaultOutputDirectory>
<processors>
<processor>org.mapstruct.ap.MappingProcessor</processor>
</processors>
</configuration>
<executions>
<execution>
<id>process</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
</plugin>



</plugins>
</pluginManagement>
</build>


<dependencies>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>de.dpt</groupId>
<artifactId>de.dpt.gen</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


<dependency>
<groupId>de.dpt</groupId>
<artifactId>de.dpt.amazonbatch</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


<dependency>
<groupId>de.dpt</groupId>
<artifactId>de.shopify.api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>

<dependency><!-- handle conversion to json; supports circular references -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>



<dependency>
<groupId>com.voodoodyne.jackson.jsog</groupId>
<artifactId>jackson-jsog</artifactId>
<version>1.1</version>
</dependency>

<!-- http://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>





<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>

<dependency>
<groupId>com.github.greengerong</groupId>
<artifactId>prerender-java</artifactId>
<version>1.6.4</version>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>


<dependency><!-- do not embed tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- don't ship this shit with the war!!! -->
<dependency><!-- do not embed tomcat -->
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>provided</scope>
</dependency>



<!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> </dependency> -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>




<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>[0.4, 0.5)</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>



</dependencies>




<version>0.0.1-SNAPSHOT</version>
</project>

最佳答案

如果您检查 Spring Boot documentation对于 spring-boot-maven-plugin 它说

Once spring-boot-maven-plugin has been included in your pom.xml it will automatically attempt to rewrite archives to make them executable using the spring-boot:repackage goal.

If you don’t specify a main class the plugin will search for a class with a public static void main(String[] args) method.

所以在你的模块“de.dpt.gen”中,这个插件可能能够找到具有 public static void main(String[] args) 方法的类,而在模块“de.shopify.api”中可能没有任何具有 public static void main(String[] args) 方法的类。

如果你想同时运行它,独立的可执行文件和正常的 war 部署,那么在你的 parent/pom.xml 中 spring-boot-maven-plugin 你可以包含 Application类的主要方法如下所示,以明确指定它。

<configuration>
<mainClass>path.of.your.Application</mainClass>
<outputDirectory>webapps</outputDirectory>
</configuration>

如果你想在外部 tomcat 中部署正常的 war ,那么你不需要在你的父 pom 中包含 spring-boot-maven-plugin 因为你正在使用 spring-boot -starter-parent 负责 war 文件的打包。

关于java - Maven:多模块 WAR 构建为库项目提供 "Unable to find main class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com