Maven是否有可能配置不同的配置



我有一个具有以下概念结构的项目:

-- Core project: Contains the main() function
---- Feature A
---- Feature B
---- Feature C
---- etc.

我正在寻找一种告诉Maven的方法:

mvn package core--with--featureA--and--featureC

此示例将创建一个从Core的main开始的可执行JAR文件,并且具有功能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有项目,然后是模块。他们可以释放一个不同的罐子。

下面的模块在其源中仅具有配置文件。它是根据依赖项的包含来构建应用程序的。它还从这些依赖项中提取其他属性,以在其构建中使用。

您将遇到的问题是,您需要使用反射来避免在主应用中构建问题。即如果无法解决包含的话,它不会编译。

ie farmaturea.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是否可以使用不同的配置来组装可执行罐?

是的。一种相对简单的方法是使用maven-ipembly-plugin

在启动罐子时,主要方法应该能够知道安装了哪些功能

这与Maven无关。它是您试图构建自己的模块系统的接缝。虽然这样做并非严格错误,但您可能需要考虑已经这样做的现有解决方案:

  • Java的服务加载程序可以是一种方法(在相对简单的情况下)。
  • OSGI实际上是模块化Java应用的Standart。我知道很多人会争论(也许由于过时的知识)太重/复杂,但事实并非如此。如果您想这样走并从真实模块化的力量中受益,则可以检查该基础教程哪个大量多模块应用程序。该示例使用BNDTOOLS,但您可以使用Maven做同样的事情。

为什么可以使其变得简单时使它变得复杂!

POM核心项目:

<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

最新更新