Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Allow specification of distributionManagement section in generated pom.xml #9069

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mzellho
Copy link
Contributor

@mzellho mzellho commented Mar 24, 2021

This PR enables the configuration to pass repository and snapshotRepository information to the generated pom.xml, so a user can publish the generated package to a maven repository.

Disclaimer: implemented as close as possible to PR #1132, since the functionality is almost identical (thank you @mwoodland, all kudos to you)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@mzellho
Copy link
Contributor Author

mzellho commented Mar 24, 2021

@mzellho mzellho changed the title Allow specification of distributionManagement section in generated pom.xml [Java] Allow specification of distributionManagement section in generated pom.xml Mar 24, 2021
@falk-stefan
Copy link

falk-stefan commented Aug 23, 2021

tumbleweed

I could really use this feature right now.

@mzellho
Copy link
Contributor Author

mzellho commented Aug 24, 2021

Hi @falk-stefan,

since this PR didn't really go anywhere (sadly!!!) I've found a way to achieve this in the meantime by creating a standalone Maven API project that we're using to build a jaxrs-spec server and a typescript-angular client via our build server.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>our.package</groupId>
    <artifactId>our-project-api</artifactId>
    <packaging>jar</packaging>
    <version>1.2.3-SNAPSHOT</version>

    <name>Our Project</name>
    <description>API for our Project</description>

    <properties>
        <!-- application properties -->
        <java.version>11</java.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <maven.compiler.parameters>true</maven.compiler.parameters>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>

        <!-- dependency versions -->

        <!-- do not update version, we want to stay in sync with what the generated artifact looks like -->
        <jackson-datatype-jsr310.version>2.9.9</jackson-datatype-jsr310.version>
        <javax.annotation-api.version>1.3.2</javax.annotation-api.version>
        <javax.ws.rs-api.version>2.1.1</javax.ws.rs-api.version>
        <swagger-annotations.version>1.5.3</swagger-annotations.version>
        <validation-api.version>1.1.0.Final</validation-api.version>
        <!-- / do not update version, we want to stay in sync with what the generated artifact looks like -->

        <!-- plugin dependencies -->
        <openapi-generator-maven-plugin.version>5.2.0</openapi-generator-maven-plugin.version>
        <build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
    </properties>

    <repositories>
        <repository>
            <!-- our repo -->
        </repository>

        <repository>
            <!-- our repo -->
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <!-- our repo -->
        </repository>

        <snapshotRepository>
            <!-- our repo -->
        </snapshotRepository>
    </distributionManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <parameters>${maven.compiler.parameters}</parameters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>${openapi-generator-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>jaxrs-spec-server</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generatorName>jaxrs-spec</generatorName>

                            <inputSpec>${project.basedir}/reference/our-api.json</inputSpec>

                            <groupId>${project.groupId}</groupId>
                            <artifactId>api-jaxrs-server</artifactId>
                            <artifactVersion>${project.version}</artifactVersion>

                            <packageName>${project.groupId}</packageName>
                            <invokerPackage>${project.groupId}</invokerPackage>
                            <apiPackage>${project.groupId}.api</apiPackage>
                            <modelPackage>${project.groupId}.model</modelPackage>

                            <configOptions>
                                <sourceFolder>src/gen/java/main</sourceFolder>
                                <interfaceOnly>true</interfaceOnly>
                                <dateLibrary>java8</dateLibrary>

                                <!-- detected via build-helper-maven-plugin -->
                                <snapshotVersion>${snapshot}</snapshotVersion>
                            </configOptions>
                        </configuration>
                    </execution>
                    <execution>
                        <id>typescript-angular-client</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generatorName>typescript-angular</generatorName>

                            <inputSpec>${project.basedir}/reference/our-api.json</inputSpec>
                            <output>${project.basedir}/typescript-angular</output>

                            <templateDirectory>${project.basedir}/templates/typescript-angular</templateDirectory>

                            <configOptions>
                                <ngVersion>12.2.0</ngVersion>

                                <npmName>@my-project/api-client-typescript-angular</npmName>
                                <npmVersion>${project.version}</npmVersion>
                                <npmRepository><!-- our repo --></npmRepository>

                                <!-- detected via build-helper-maven-plugin -->
                                <snapshot>${snapshot}</snapshot>

                                <fileNaming>kebab-case</fileNaming>
                                <apiModulePrefix>OurProject</apiModulePrefix>
                                <stringEnums>true</stringEnums>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${build-helper-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>snapshot-detection</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>regex-property</goal>
                        </goals>
                        <configuration>
                            <name>snapshot</name>
                            <value>${project.version}</value>
                            <regex>.*-SNAPSHOT</regex>
                            <replacement>true</replacement>
                            <failIfNoMatch>false</failIfNoMatch>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${javax.ws.rs-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>${validation-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-annotations.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>${javax.annotation-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson-datatype-jsr310.version}</version>
        </dependency>
    </dependencies>

</project>

I removed some bits like a HTML2 and a custom AsciiDoc Generator, as well as some project-specific information, but something like this should be working.

During the build, we're simply calling mvn clean javadoc:jar source:jar deploy for the server part and following snippet for the client part:

npm install
npm run build
cd dist
npm publish

Credentials for pushing the libraries to our registry (Artifactory in our case) are injected via a custom settings.xml and .npmrc via our build server.

HTH,
Max

@falk-stefan
Copy link

Hi! Thanks a lot for sharing!

I am not sure if this can work in my case though. I'm trying to publish a package to GitHub packages (see).

The docs state:

For a Maven-based project, you can make use of these settings by creating a distribution repository in your pom.xml file with an id of github that points to your GitHub Packages endpoint.

For example, if your organization is named "octocat" and your repository is named "hello-world", then the GitHub Packages configuration in pom.xml would look similar to the below example.

<project ...>
 ...
 <distributionManagement>
   <repository>
     <id>github</id>
     <name>GitHub Packages</name>
     <url>https://maven.pkg.github.com/octocat/hello-world</url>
   </repository>
 </distributionManagement>
</project>

So, I think in order to automate this, I somehow need OpenAPI to set distributionManagement directly in the final pom.xml and I'm afraid that this is also the case for my other issue where I want to publish a npm package to GitHub.

@mzellho
Copy link
Contributor Author

mzellho commented Aug 24, 2021

Hi @falk-stefan,

you're welcome.

I never maven published to GitHub but we're also using distributionManagement for publishing to our Artifactory, so I'd fancy that this should be working quite identically.

As for NPM, which we also publish to our Artifactory (instead of GitHub or NPM), we had to learn some things about scoped packages and .npmrc in general, but basically, it's the same story there (https://docs.github.com/en/actions/guides/publishing-nodejs-packages).

@falk-stefan
Copy link

Ah, silly me. I think I misunderstood what you did at first. Now I get it. You're right, this should work. Just let the CI generate the code and publish it.

Thanks a lot! :)

@gallardo
Copy link

You can achieve this by using mvn -DaltDeploymentRepository=id::url deploy (thanks https://stackoverflow.com/a/62022779/413020).

Another much less convenient option is using parentArtifactId, parentGroupId and parentVersion to inherit from a pom of yours with appropriate distributionManagement configuration, but the generated jar will also keep that dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants