简体   繁体   English

composer 从两个 composer.json 文件安装

[英]composer install from two composer.json file

I have multiple composer.json having multiple seperate dependency and want to install all the dependency in the both composer.json using single composer install command.我有多个composer.json具有多个单独的依赖项,并希望使用单个composer install命令在两个composer.json中安装所有依赖项。

The location is like this:位置是这样的:

| - composer.json
| - Custom
    | - Package1
        | - composer.json

First composer.json第一作曲家.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "barryvdh/laravel-debugbar": "^2.3",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "^5.4.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
        }
    },
    "autoload-dev": {
        "psr-4": {
          "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

Second composer.json inside Package1 directory Package1目录中的第二个composer.json

{
    "name": "custom/package1",
    "description": "",
    "require": {
        "php": ">=5.6",
        "composer/installers": "~1.0",
        "lavary/laravel-menu": "1.7.1"
    },
    "autoload": {
        "psr-4": {
            "Custom\\Package1\\": ""
        }
    }
}

I want to install the lavary/laravel-menu inside the Package1 to the main vendor directory where all the default packages are installed.我想将 Package1 中的Package1 lavary/laravel-menu安装到安装了所有默认包的主供应商目录中。

|- vendor  //<==want here
| - composer.json
    | - Custom
        | - Package1
            | - vendor  //<== not here
            | - composer.json

I have tested this solution:我已经测试了这个解决方案:

https://stackoverflow.com/a/27735674 https://stackoverflow.com/a/27735674

like this:像这样:

{
    "config": {
        "vendor-dir": "../../vendor/"
    }
}

This installs the packages but we need to get into the second composer.json instead of main composer.json and removes the installed package from first composer.json. This installs the packages but we need to get into the second composer.json instead of main composer.json and removes the installed package from first composer.json.

How can i install the all the dependency from the main composer.json without getting into the second or multiple composer.json in single vendor directory?如何在不进入单个供应商目录中的第二个或多个 composer.json 的情况下安装来自主 composer.json 的所有依赖项?

After some research and suggestion i figured out there are multiple ways to achieve this solution. 经过一些研究和建议,我发现有多种方法可以实现这个解决方案。

  1. Using external package to maintain the dependency. 使用外部包来维护依赖性。

Thanks to rickdenhaan to let me know about 感谢rickdenhaan让我知道

Composer Merge Plugin Composer Merge插件

https://github.com/wikimedia/composer-merge-plugin https://github.com/wikimedia/composer-merge-plugin

First we need to require this package: 首先,我们需要这个包:

composer require wikimedia/composer-merge-plugin

Then my composer.json becomes like this: 然后我的composer.json变成这样:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "barryvdh/laravel-debugbar": "^2.3",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "wikimedia/composer-merge-plugin": "^1.4"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
          "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "extra": {
        "merge-plugin": {
            "include": [
                "Custom/*/composer.json"
            ],
            "recurse": true,
            "replace": false,
            "ignore-duplicates": true,
            "merge-dev": true,
            "merge-extra": false,
            "merge-extra-deep": false,
            "merge-scripts": true
        }
    }
}

Now, run 现在,跑

composer install

or 要么

 composer update

Then your composer.json in each of the directory will be merged into default vendor directory. 然后,每个目录中的composer.json将合并到默认vendor目录中。

  1. Next solution could be to publish the package to the packagist and require in the composer.json and during composer install all the dependency will be installed. 下一个解决方案可能是将包发布到packagist并在composer.json中需要,并且在composer安装期间将安装所有依赖项。

    Like the Asgardcms has done. 就像Asgardcms所做的那样。

  2. Adding the private repository to the composer.json. 将私有存储库添加到composer.json。

    Configuring composer.json with private bitbucket mercurial repository 使用私有bitbucket mercurial存储库配置composer.json

  3. This option needs to add the composer.json of metapackage to the main composer.json file. 此选项需要将metapackage的composer.json添加到主composer.json文件中。

(I am not sure about the option but thanks to JParkinson1991 Someone who knows this solution could add explaination on this option. Adding just to let someone know this solution exists.) (我不确定这个选项,但感谢JParkinson1991知道这个解决方案的人可以在这个选项上添加解释。只是为了让别人知道这个解决方案存在。)

Here is the example solution: 以下是示例解决方案:

PHP & Composer, how do I combine composer.json files PHP和Composer,我如何组合composer.json文件

In this the first solution suits my case. 在这里,第一个解决方案适合我的情况。 Hope this helps someone who spent alot of time searching. 希望这可以帮助花费大量时间搜索的人。

Here is an alternative way that hopefully serves the use case of multiple composer JSON files.这是一种替代方法,有望为多个作曲家 JSON 文件的用例提供服务。

Generally, we want to use multiple JSON files to differentiate production & development packages.通常,我们希望使用多个 JSON 文件来区分生产和开发包。

Packages that require in development only, we can use with require-dev仅在开发中需要的软件包,我们可以与require-dev一起使用

Here is an example of composer file:
{
    "name": "name",
    "type": "library",
    "description": "Dummy Description",
    "autoload": {
        "psr-4": {
            "Vendor\\Cache\\":"cache/",
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^9",
        "yoast/phpunit-polyfills": "^1.0"
    }
}

So now if we want to use only production packages then use below command:所以现在如果我们只想使用生产包,那么使用下面的命令:

composer install --no-dev (It will not install packages that is inside require-dev)

If we need both development & production packages then use below command.如果我们需要开发和生产包,那么使用下面的命令。

composer install (It will install all the packages)

It is just a suggestion.这只是一个建议。 Hopefully, it may be helpful.希望它可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM