Custom install path for composer package

Recently I had to build a custom Composer package that had to be installed to a custom install path inside of the target application.
But the install path should have been only different for this single package. All other Composer packages had to be installed unter the vendor folder furthermore.

Custom install path for composer package

Requirements

I had the following requirements to the package:

  • should be compatible with the satis Composer repository of my company
  • the library/tool (mina multienv deployment -> article and code will be published a.s.a.p.) should be installed outside of the defualt vendor folder
  • should use as much Composer standard as possible
  • should use other packages if possible

Solution for using a custom install path for a single package

The solution was to use the existing Composer package composer installer and utilize it for my own purpose. Composer installer is a Composer plugin, that serves package types for common PHP applications and frameworks such as Symfony2, Zend, DokuWiki and also Magento.
Besides this package has a really useful feature. It allows any other Composer package, that is based on it to define a custom install path to which the derivated Composer package will be copied during updates or installations for the target application.  And that was exactly what I was looking for.

Steps to success

All I had to do to get a custom install path for my module was was:

  1. Require the composer installer in my own packages composer.json

    "require": {
      "composer/installers": "~1.0"
    }
  2. Gave it a special installer name (in my packages composer.json):
    "extra": {
      "installer-name": "mina"
    }

    The folder to which the package will be copied to will be named after it (mina).

  3. Set a “fake” – package type:
    {
      "name": "sheldon/mina_multienv_deployment",
      "description": "Mina deployment tool",
      "type" : "cakephp-plugin",
      "license": "OSL-3.0",
      "authors": [
        {
          "name": "Marcel Lange",
          "email": "info@ask-sheldon.com",
          "hompage": "https://www.ask-sheldon.com",
          "role": "Developer"
        }
      ],
      "keywords": [
        "Deployment",
        "Multiserver Environment",
        "Mina"
      ],
    
      ...
    
    }

    As you can see, I simply misused the cakephp-plugin repository type delivered by composer installer, although my code isn’t a cakephp plugin at all. I did this, because I had no time to implement a custom vendor type so far and it was not absolutely  necessary to achieve my goals mentioned above. If you want to know, how you can create own vendor types, please have a look at linked tutorials below.

  4. Require the package in the composer.json of the target application:
    "repositories": [
      {
        "type": "composer",
        "url": "https://repository.git-platform.tld/mina_multienv_deployment.git"
      }
    ],
    "minimum-stability": "dev",
    "extra": {
      "installer-paths": {
        "../{$name}/": ["sheldon/mina_multienv_deployment"]
      }
    }

That’s it! If you run $> composer update for the targeted application, the Composer package will be installed to ../mina/ relative to you composer.json.

Further information

If you need more that a custom install path, you should have a look at these resources, that helped me a lot to get started:

2 thoughts on “Custom install path for composer package

  1. Hello,

    I tried to this but it doesn’t work.
    Can you give your full file composer.json, please?

    Thank you.

    1. Hello Anardil,
      here is the complete composer.json of the application in satis repo:


      {
      "name": "sheldon/mina_multienv_deployment",
      "description": "Mina deployment tool",
      "type" : "cakephp-plugin",
      "license": "OSL-3.0",
      "version": "v1.1.9",
      "authors": [
      {
      "name": "Marcel Lange",
      "email": "info@ask-sheldon.com",
      "hompage": "https://www.ask-sheldon.com",
      "role": "Developer"
      }
      ],
      "keywords": [
      "Deployment",
      "Multiserver Environment",
      "Mina"
      ],
      "require": {
      "composer/installers": ">=1.0"
      },
      "extra": {
      "installer-name": "mina"
      }
      }

      And that’s the composer.json on the target application:

      {
      "name": "sheldon/project_name",
      "description": "Project",
      "license": "OSL-3.0",
      "require": {
      "magento-hackathon/magento-composer-installer": "*",
      "sheldon/mina_multienv_deployment": ">=1.1.8"
      },
      "repositories": [
      {
      "type": "composer",
      "url": "https://packages.firegento.com"
      },
      {
      "type": "composer",
      "url": "https://satis.ask-sheldon.com"
      }
      ],
      "extra": {
      "magento-root-dir": "../../mage/",
      "magento-deploystrategy": "copy",
      "magento-force": true,
      "installer-paths": {
      "../{$name}/": ["sheldon/mina_multienv_deployment"]
      }
      }
      }

      I hope that will bring you further.
      Keep in mind, that I have an own composer repository. That dosn’t work with the default ones only.

      Kind regards,
      Marcel

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.