[ 
https://issues.apache.org/jira/browse/CB-9704?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16981721#comment-16981721
 ] 

Ross Bender commented on CB-9704:
---------------------------------

I agree that it's unfortunate there's no built-in way to do this. Building on 
an [existing SO answer|https://stackoverflow.com/a/36210134], we're using a 
hook that will add a custom repository to any app/plugin gradle files:

{code}
const fs = require("fs");
const path = require("path");
const async = require("async");

module.exports = context => {
    "use strict";
    const repoUrl = "http://nexus.corp.aal.au/content/groups/public-ad";;
    const gradleRepo = 'maven { url "' + repoUrl + '" }';
    return new Promise((resolve, reject) => {
        const platformRoot = path.join(context.opts.projectRoot, 
"platforms/android");

        const gradleFiles = findGradleFiles(platformRoot);

        async.each(
            gradleFiles,
            function(file, callback) {
                let fileContents = fs.readFileSync(file, "utf8");

                let insertLocations = [];
                const myRegexp = /\brepositories\s*{(.*)$/gm;
                let match = myRegexp.exec(fileContents);
                while (match != null) {
                    if (match[1].indexOf(repoUrl) < 0) {
                        insertLocations.push(match.index + match[0].length);
                    }
                    match = myRegexp.exec(fileContents);
                }

                if (insertLocations.length > 0) {
                    insertLocations.reverse(); // process locations end -> 
beginning to preserve indices
                    insertLocations.forEach(location => {
                        fileContents =
                            fileContents.substr(0, location) +
                            gradleRepo +
                            fileContents.substr(location);
                    });

                    fs.writeFileSync(file, fileContents, "utf8");
                    console.log("updated " + file + " to include repo " + 
repoUrl);
                }

                callback();
            },
            function(err) {
                if (err) {
                    console.error("unable to update gradle files", err);
                    reject();
                } else {
                    resolve();
                }
            },
        );
    });

    function findGradleFiles(dir) {
        let results = [];
        const list = fs.readdirSync(dir);
        list.forEach(fileName => {
            const filePath = path.join(dir, fileName);
            const stat = fs.statSync(filePath);
            if (stat && stat.isDirectory()) {
                // recurse into subdirectory
                results = results.concat(findGradleFiles(filePath));
            } else if (path.extname(filePath) === ".gradle") {
                results.push(filePath);
            }
        });
        return results;
    }
};
{code}

We then use this as a hook for our Android platform:

{code}
<hook src="build/android/useInternalRepo.js" type="before_build" />
{code}

Hopefully this will help others, or be a starting point for getting this built 
into Cordova natively.

> Apache Cordova 5 does not support using a custom nexus repository for android 
> builds
> ------------------------------------------------------------------------------------
>
>                 Key: CB-9704
>                 URL: https://issues.apache.org/jira/browse/CB-9704
>             Project: Apache Cordova
>          Issue Type: New Feature
>          Components: Ionic
>            Reporter: Ajay Gupta
>            Priority: Major
>
> We are using AngularJS and Ionic for writing a mobile application supported 
> both on IOS and Android.  When we do "ionic build android --release", it 
> creates a build.gradle file in both platforms/android directory and 
> platforms/android/CordovaLib directory.  Both of these files point to 
> mavenCentral() as the repository which tries to download artifacts directly 
> from the maven central repo (http://repo1.maven.org/maven2).  We are trying 
> to build our mobile apps as part of a Jenkins build behind company proxy and 
> would like to have gradle reach out to our internal Maven nexus repository 
> instead of reaching out to the central maven repository directly.  Our 
> internal Neux repository would then proxy everything to the maven central 
> repository.  
> We cannot hand edit the build.gradle file as it is a generated file and so 
> any custom changes made to it would be lost in the next build.  Cordova 5 
> supports adding custom extensions using build-extras.gradle file but it does 
> not support overriding repositories.  
> As a workaround, we have defined a pre-build Cordova hook to search replace 
> mavenCentral() references to our local maven repository before doing an 
> android build.  It works but is a unnecessary workaround and not a very clean 
> solution.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to