maven-source-plugin not work for kotlin

6.6k Views Asked by At

I am trying to use maven-source-plugin to create a source.jar for my kotlin project, but seems the maven-source-plugin not work well for kotlin project.

when i run "mvn source:jar", the output message always says:

[INFO] No sources in project. Archive not created.

here is the maven-source-plugin configuration in my pom file of the project:

    <build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <attach>true</attach>
                        <includes>
         <!-- i am trying to specify the include dir manually, but not work -->                               
                         <include>${project.basedir}/src/main/kotlin/*</include>
                        </includes>
                        <forceCreation>true</forceCreation>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

my question is: how to attach kotlin source files using maven-source-plugin?

thanks ~~

2

There are 2 best solutions below

2
On BEST ANSWER

By default maven expects sources to be in src/main/java directory. If you use non-default directories, you have to specify them in build element:

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>
</build>
2
On

When your project has mixed Java and Kotlin (i.e. multiple source roots), I found that using the build-helper-maven-plugin worked well to ensure that both Java and Kotlin sources are included in the built sources artifact.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.0.0</version>

  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/main/kotlin</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>