How to Add an Extra Source Directory for Maven to Compile and Include in the Build Jar

How to add an extra source directory for maven to compile and include in the build jar?

You can use the Build Helper Plugin, e.g:

<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Maven compile with multiple src directories

You can add a new source directory with build-helper:

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to add local jar files to a Maven project?

Install the JAR into your local Maven repository (typically .m2 in your home folder) as follows:

mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true

Where each refers to:

<path-to-file>: the path to the file to load e.g → c:\kaptcha-2.3.jar

<group-id>: the group that the file should be registered under e.g → com.google.code

<artifact-id>: the artifact name for the file e.g → kaptcha

<version>: the version of the file e.g → 2.3

<packaging>: the packaging of the file e.g. → jar

Reference

  • Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
  • Maven Install Plugin Usage: The install:install-file goal

How to include custom folder in Maven Package?

In the build section of your pom.xml, under the maven war plugin declaration you can add the necessary configuration in which you specify your web resource as a directory and it's output path too:

       <plugin>
<artifactId>maven-war-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven-war-plugin.version}</version>
<configuration>
<webResources>
<resource>
<directory>META-INF</directory>
<filtering>true</filtering>
<targetPath>META-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

Or by adding it as a resource directly under build :

<build>
....
<resources>
<resource>
<directory>META-INF</directory>
</resource>
</resources>
</build>

Maven custom directory structure - no sources to compile despite specifying sourceDirectory

If I understand your issue correctly, You have an application folder and the actual source (java) folder is from somewhere else in the file system.
And you linked the external folder as java source through eclipse for compilation.

By linking in Eclipse, maven will not automatically know where the source files are. Maven follows standard directory structure for looking up java and test files.
You can use this Build Helper plugin to customize the way maven looks up sources.

An example talked here

Maven: best way of linking custom external JAR to my project?

I think you should use mvn install:install-file to populate your local repository with the library jars then you should change the scope from system to compile.

If you are starting with maven I suggest to use maven directly not IDE plugins as it adds an extra layer of complexity.

As for the error, do you put the required jars on your classpath? If you are using types from the library, you need to have access to it in the runtime as well. This has nothing to do with maven itself.

I don't understand why you want to put the library to source control - it is for sources code not binary jars.

Maven can't build a project with multiple source directories

Your parent-pom shows <packaging>pom</packaging> so the sources you define in the parent pom will not be compiled.



Related Topics



Leave a reply



Submit