Is it possible to generate(mine) a block with specific transaction via bitcoin-cli on regtest? I want to generate a block with specific transaction via bitcoin-cli on regtest.

The "generate" command does not have the option. It collects transactions in mempool by the default rule.

I guess I can do it with some code change like the following way but I don't want to change it if possible.

TestChain100Setup::CreateAndProcessBlock(const std::vector&lt;CMutableTransaction&gt;&amp; txns, const CScript&amp; scriptPubKey)
{
    const CChainParams&amp; chainparams = Params();
    std::unique_ptr&lt;CBlockTemplate&gt; pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
    CBlock&amp; block = pblocktemplate-&gt;block;

    // Replace mempool-selected txns with just coinbase plus passed-in txns:
    block.vtx.resize(1);
    for (const CMutableTransaction&amp; tx : txns)
        block.vtx.push_back(MakeTransactionRef(tx));


https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/miner.cpp#L147
https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/test/test_bitcoin.cpp#L155
https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generate/
When you run generate, it mines a block with the transactions in the mempool:

generate nblocks ( maxtries )

Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.


If you want those new coins to be mined to a specific address, you can use generatetoaddress

You can create transactions and submit them to the mempool either using the wallet RPC, e.g. sendtoaddress or using the rawtransactions RPC:
e.g. 


createrawtransaction
signrawtransaction
sendrawtransaction


sendrawtransaction will put the transaction in the mempool, which can then be included in the next block using generate