WildFly Swarm 不会在 Multi-Module Maven 项目中生成 swarm-jar
WildFly Swarm does not generate swarm-jar in Multi-Module Maven Project
我有一个多 maven 项目设置,你可以在下面找到我的 poms 和我的 Main class:
Maven 项目结构:
Parent
-- pom.xml
-- 网页
----- pom.xml
Parent pom:
...
<modules>
<module>web</module>
</modules>
<packaging>pom</packaging>
...
Web-Module 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>amazon-fba</artifactId>
<groupId>de.domain</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<artifactId>web</artifactId>
<dependencies>
<!-- Provided -->
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>logging</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jsf</artifactId>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>infinispan</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs-jsonp</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>h2</artifactId>
<version>2016.11.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<mainClass>de.package.Main</mainClass>
</configuration>
<executions>
<execution>
<id>package</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的主要class:
public class Main {
private static final String WEBAPP_SRC = "web/src/main/webapp";
public static void main(String[] args) throws Exception {
Swarm swarm = new Swarm(args);
swarm = buildSwarmFractions(swarm);
WARArchive warArchive = buildDeployment();
swarm.start()
.deploy(warArchive);
}
private static Swarm buildSwarmFractions(Swarm swarm) {
return swarm
.fraction(new DatasourcesFraction()
.dataSource(new DataSource(...)
.connectionUrl(...)
.driverName(...)
.userName(...)
.password(...)));
}
private static WARArchive buildDeployment() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL persistenceFile = classLoader.getResource("META-INF/persistence.xml");
return ShrinkWrap.create(WARArchive.class)
.addAsResource(new File(persistenceFile.toURI()), "META-INF/persistence.xml")
.addAllDependencies()
.addPackages(true, "de.mypackage")
.merge(ShrinkWrap.create(GenericArchive.class)
.as(ExplodedImporter.class)
.importDirectory(WEBAPP_SRC).as(GenericArchive.class), "/", Filters.includeAll());
}
}
我可以 运行 来自 IntelliJ 的 Main class 没有任何问题。我在 parent 和 web 文件夹中尝试了几个 maven 命令。但是,我无法生成 -swarm.jar (Uberjar)。我尝试了不同的包装 (jar/war) 但没有成功。我想要实现的是:
- 能够运行来自 IntelliJ 的 Main 方法
- 生成一个我可以调用的 Uberjar (java -jar)
WildFly Swarm 文档指出,运行宁一个 mvn 包应该生成 uberjar。我在文档中遗漏了什么吗?
感谢任何帮助。
-swarm.jar 没有生成,因为我在 web/pom.xml
中执行错误
<build>
<finalName>web</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<mainClass>de.kevcodez.amazonfba.Main</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
更改此后,我还必须更改 Main class 才能正确部署。这是我的工作 Main class:
public static void main(String[] args) throws Exception {
Swarm swarm = new Swarm(args);
swarm = buildSwarmFractions(swarm);
WebArchive webArchive = ShrinkWrap.createFromZipFile(WebArchive.class, new File("web/target/web.war"));
swarm.start()
.deploy(webArchive);
}
private static Swarm buildSwarmFractions(Swarm swarm) {
return swarm
.fraction(new DatasourcesFraction()
.dataSource(...));
}
我有一个多 maven 项目设置,你可以在下面找到我的 poms 和我的 Main class:
Maven 项目结构:
Parent
-- pom.xml
-- 网页
----- pom.xml
Parent pom:
...
<modules>
<module>web</module>
</modules>
<packaging>pom</packaging>
...
Web-Module 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>amazon-fba</artifactId>
<groupId>de.domain</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<artifactId>web</artifactId>
<dependencies>
<!-- Provided -->
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>logging</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jsf</artifactId>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>ejb</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>infinispan</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs-jsonp</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>h2</artifactId>
<version>2016.11.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<mainClass>de.package.Main</mainClass>
</configuration>
<executions>
<execution>
<id>package</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的主要class:
public class Main {
private static final String WEBAPP_SRC = "web/src/main/webapp";
public static void main(String[] args) throws Exception {
Swarm swarm = new Swarm(args);
swarm = buildSwarmFractions(swarm);
WARArchive warArchive = buildDeployment();
swarm.start()
.deploy(warArchive);
}
private static Swarm buildSwarmFractions(Swarm swarm) {
return swarm
.fraction(new DatasourcesFraction()
.dataSource(new DataSource(...)
.connectionUrl(...)
.driverName(...)
.userName(...)
.password(...)));
}
private static WARArchive buildDeployment() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL persistenceFile = classLoader.getResource("META-INF/persistence.xml");
return ShrinkWrap.create(WARArchive.class)
.addAsResource(new File(persistenceFile.toURI()), "META-INF/persistence.xml")
.addAllDependencies()
.addPackages(true, "de.mypackage")
.merge(ShrinkWrap.create(GenericArchive.class)
.as(ExplodedImporter.class)
.importDirectory(WEBAPP_SRC).as(GenericArchive.class), "/", Filters.includeAll());
}
}
我可以 运行 来自 IntelliJ 的 Main class 没有任何问题。我在 parent 和 web 文件夹中尝试了几个 maven 命令。但是,我无法生成 -swarm.jar (Uberjar)。我尝试了不同的包装 (jar/war) 但没有成功。我想要实现的是:
- 能够运行来自 IntelliJ 的 Main 方法
- 生成一个我可以调用的 Uberjar (java -jar)
WildFly Swarm 文档指出,运行宁一个 mvn 包应该生成 uberjar。我在文档中遗漏了什么吗?
感谢任何帮助。
-swarm.jar 没有生成,因为我在 web/pom.xml
中执行错误 <build>
<finalName>web</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<mainClass>de.kevcodez.amazonfba.Main</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
更改此后,我还必须更改 Main class 才能正确部署。这是我的工作 Main class:
public static void main(String[] args) throws Exception {
Swarm swarm = new Swarm(args);
swarm = buildSwarmFractions(swarm);
WebArchive webArchive = ShrinkWrap.createFromZipFile(WebArchive.class, new File("web/target/web.war"));
swarm.start()
.deploy(webArchive);
}
private static Swarm buildSwarmFractions(Swarm swarm) {
return swarm
.fraction(new DatasourcesFraction()
.dataSource(...));
}