Where in maven project's path should I put configuration files that are not considered resources

前端 未结 4 1251
温柔的废话
温柔的废话 2020-12-23 09:55

I have a simple java maven project. One of my classes when executing needs to load an xml configuration file from the classpath. I don\'t want to package such xml file when

相关标签:
4条回答
  • 2020-12-23 10:08

    You could place it in src/test/conf/default.xml. Your testclasses can find it, but it wont be packaged using the standard method.

    With an additional assembly you can package it from there. That step is always necessary.

    A different solution could be to create a separate maven module and place it in /src/main/resources/conf/... .Then make this jar a test dependency. You do not need to do any special plugin configuration, but I think it is overkill for a single file.

    0 讨论(0)
  • 2020-12-23 10:16

    If your packaging is war, you can use the packagingExcudes configuration option of the maven-war-plugin:

    <project>
      ...
      <build>
        <plugins>
          ...
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
              <!-- Exclude abc.properties found in src/main/resources/ (ends up getting packaged in WEB-INF/classes/) -->
              <packagingExcludes>
                WEB-INF/classes/abc.properties
              </packagingExcludes>
            </configuration>
          </plugin>
          ...
        </plugins>
      </build>
      ...
    </project>
    

    Use commas to separate between multiple resources you want to exclude. Also, you can use wildcards and regex in your excluded paths. For regex, it's in the %regex[YOUR_REGEX_HERE] syntax. Check the documentation for more details.

    0 讨论(0)
  • 2020-12-23 10:19

    My solution was to use two profiles: Development (default) and Packaging

    My default / section contains both src/main/resources and src/main/conf. I call this my Development profile, which is an implicit profile.

    My packaging profile is an explicit profile which is defined under section. There under / I only mentioned src/main/resources. When I'm running my packaging script (we currently have this external to maven since its building an RPM out of our WAR), I'm running 'mvn install -Drpm' to activate my Packaging profile (rpm is the id for the Packaging profile.

    If this wasn't clear enough, feel free to ask more questions.

    0 讨论(0)
  • 2020-12-23 10:31

    The question is what is the best solution of the two? If the correct is 2, what is the best way to copy it to target folder? Is there any other solution better and more common than those two?

    Since you want that file to be copied to the target/classes folder, it has somehow to be considered as a resource (so either put in under src/main/resources or declare src/main/conf as resource directory). And if you don't want it in the final jar, configure the Maven JAR Plugin to exclude it:

    <project>
      ...
      <build>
        <plugins>
          ...
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
              <excludes>
                <exclude>**/conf/*</exclude>
              </excludes>
            </configuration>
          </plugin>
          ...
        </plugins>
      </build>
      ...
    </project>
    

    For the assembly part, assembly descriptors are pretty flexible so it should be possible to achieve what you want regardless of the choice. I'd suggest using the easiest setup though.

    0 讨论(0)
提交回复
热议问题