[英]Is it possible for Maven to assembly executable JARs with different configurations?
我有一個具有以下概念結構的項目:
-- Core project: Contains the main() function
---- Feature A
---- Feature B
---- Feature C
---- etc.
我正在尋找一種方法來告訴Maven以下內容:
mvn package core--with--featureA--and--featureC
本示例將創建一個可執行的JAR文件,該文件從Core的main
,並且還打包/組裝了功能A和C,但沒有打包功能B和其他功能。
在啟動JAR時, main
方法應該能夠知道已安裝了哪些功能並對其進行引導,例如:
main() {
for(Runnable featureStarter : FeatureList.getFeatures()) { // Gets all features assembled by maven, which are now present in runtime
featureStarter.run(); // Starts each feature
}
}
或者,一個更粗糙/硬編碼的示例(不那么受歡迎):
main() {
if(isInstalled("FeatureA"))
FeatureA.start();
if(isInstalled("FeatureB"))
FeatureB.start();
if(isInstalled("FeatureC"))
FeatureC.start();
}
這可能嗎?
謝謝!
Maven有項目,然后有modules..so,因此您可以創建2個取決於基礎項目的發行模塊。 他們可以釋放另一個罐子。
下面的模塊在其源中僅包含配置文件。 它基於包含的依賴關系構建應用程序。 它還從那些依賴項中提取其他屬性以在其構建中使用。
您將遇到的問題是,您將需要使用反射來避免主應用程序中的構建問題。 即,如果無法解析包含,則不會編譯。
即FeatureA.start()
public void Start(){
try {
clazz = Class.forName("org.group.ComponentA");
} catch (ClassNotFoundException e) {
if (fallbackOk) {
clazz = Class.forName("org.group.Component");
} else {
throw e;
}
}
}
以下POM
<?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">
<parent>
<artifactId>parent-artifact</artifactId>
<groupId>com.group</groupId>
<version>1.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child-artifact</artifactId>
<packaging>war</packaging>
<name>Child Jar</name>
<build>
<finalName>Child</finalName>
<resources>
<resource>
<directory>${basedir}/src/conf/log4j</directory>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/conf/hibernate</directory>
<includes>
<include>hibernate.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<!-- extracts the messages*.properties files from to a staging area -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group</groupId>
<artifactId>componentA</artifactId>
<version>${project.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/localisation</outputDirectory>
<includes>**/messages*.properties</includes>
</artifactItem>
<artifactItem>
<groupId>com.group</groupId>
<artifactId>componentB</artifactId>
<version>${project.version}</version>
<type>war</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/webapp/webstart</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- copies the messages*.properties files to classes/localisation -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/localisation</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/localisation/org/group/web/resource/localisation/
</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<!-- copy webapp for tomcat plugin -->
<id>webapp</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/webapp</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/webapp/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>
${basedir}/src/webapp
</warSourceDirectory>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<overlays>
<overlay>
<groupId>org.group</groupId>
<artifactId>componentC</artifactId>
<targetPath>webstart</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>app-tomcat</server>
<!--port>${maven.tomcat.port}</port-->
<path>/${project.build.finalName}</path>
<warSourceDirectory>${basedir}/target/webapp</warSourceDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>${jdbc.groupId}</groupId>
<artifactId>${jdbc.artifactId}</artifactId>
<version>${jdbc.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>app-tomcat</server>
<!--port>${maven.tomcat.port}</port-->
<path>/${project.build.finalName}</path>
<warSourceDirectory>${basedir}/target/webapp</warSourceDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>${jdbc.groupId}</groupId>
<artifactId>${jdbc.artifactId}</artifactId>
<version>${jdbc.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<!-- for jetty plugin dependencies -->
<id>java.net</id>
<url>http://download.java.net/maven/2/</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.group</groupId>
<artifactId>componentA</artifactId>
</dependency>
<dependency>
<groupId>org.group</groupId>
<artifactId>componentB</artifactId>
</dependency>
</dependencies>
<properties>
</properties>
</project>
Maven是否可以組裝具有不同配置的可執行JAR?
是的。 一種相對簡單的方法是使用maven-assembly-plugin
啟動JAR時,主要方法應該能夠知道已安裝了哪些功能
這與Maven無關。 它表明您正在嘗試構建自己的模塊系統。 這樣做並非完全錯誤,但您可能需要考慮已經做到這一點的現有解決方案:
當您可以簡化時,為什么要使其復雜化!
Pom Core項目:
<profile>
<id>active-Feature-A</id>
...
...
</profile>
<profile>
<id>active-Feature-B</id>
...
...
</profile>
<profile>
<id>active-Feature-C</id>
...
...
</profile>
然后:
mvn package -Pactive-Feature-A,active-Feature-B
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.